2011-02-10 7 views
2

next()prev()은 연관 배열에서 작동합니까?연관 배열 포인터/순회 연관 배열

두 개의 레코드를 사용하여 하나의 "게임"을 설명하는 데이터 집합을 탐색하려고합니다. 그래서 내가 두 번째 레코드 승/일치하는 ID에 내가 전에 레코드를보고하고 eg_item['final_score'] 잡아야합니다.

{"id":"75", "team_name":"TEAM1", "home_team_name":"TEAM1", "image":"TEAM1_HOME.png", "final_score":"37"}, 
{"id":"75", "team_name":"TEAM2", "home_team_name":"TEAM2", "image":"TEAM2_AWAY.png", "final_score":"10"}, 
{"id":"76", "team_name":"TEAM1", "home_team_name":"TEAM1", "image":"TEAM1_HOME.png", "final_score":"10"}, 
{"id":"76", "team_name":"TEAM2", "home_team_name":"TEAM2", "image":"TEAM2_AWAY.png", "final_score":"14"}, 

난 그냥 도움이되지 않습니다 사용 절름발이 array('one','two',three') 유형의 예제를 찾는거야 모든 예.

코드 샘플 :

foreach($json_output as $eg_item) : 

    if($this_game_id == $last_game_id) : 
     // get this records info 
     $b_score = $eg_item['final_score']; 
     $b_team_name = $eg_item['team_name']; 
     prev($json_output); 
      // get previous records info 
     $a_score = $eg_item['final_score']; 
     $a_team_name = $eg_item['team_name']; 
     $a_game_id = $eg_item['id']; 
     // put pointer back 
     next($json_output);  
    else : 
     // skip next record  
    endif; 

endforeach; 
+1

예 그들은 ASSOC 배열에서 작동합니다. 몇 가지 코드를 보여줄 수 있습니까? – netcoder

답변

0

연관 배열의 배열처럼 보이는, 맞죠? 단지를 heres, 미세한 작은

for ($i = 1; $i < count($array); $i+= 2) { 
    $current = $array[$i]; 
    $last = $array[$i-1]; 
    //$current['final_score'], $last['final_score'], etc 
} 
+0

그럴거야. 내가 읽고있는 것에서, 내 큰 피드 크기가 주어진다면 나는 어쨌든 사용하는 것이 더 낫다. 매우 감사! –

0

예 그들이 작동 : 그렇다면, 한 번에 인덱스 2에 의해 그들을 통해 루프, 당신은 현재 내부 배열과 루프의 각 반복에서 이전 내부 배열을 볼 수 있습니다 테스트 케이스는 내가이 짓을 :

<?php 
$sample = array(
    "first" => Array("a","d","c"), 
    "second" => Array("a","d","c"), 
    "third" => Array("a","d","c"), 
    "fourth" => Array("a","d","c") 
); 

while(next($sample)) 
{ 
    $item = current($sample); 
    echo key($sample) . "\n"; 

    if(is_array($item)) 
    { 
     echo "\t" . current($item) . "\n"; 
     while(next($item)) 
     { 
      echo "\t" . current($item) . "\n"; 
     } 
    } 
} 
?> 

출력 : http://codepad.org/2ZeqzWcx