2011-02-10 39 views
2

请问next()prev()对关联数组有效吗?关联数组指针/遍历关联数组

我想遍历数据集,如果你愿意的话,它将使用两条记录来描述一个“游戏”。所以当我登录第二张唱片时,我需要先查看唱片并抓取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 2011-02-10 16:28:10

回答

0

这看起来像关联数组的数组,是吗?如果是这样,循环通过他们通过索引2的时间,那么你可以看看现在的内部数组,并在每次循环之前的内部数组:

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

那会做丹。从我正在阅读的内容来看,考虑到我的大尺寸饲料,我最好不要使用它。非常感激! – 2011-02-10 18:31:43

0

是的,他们工作得很好,继承人小测试情况下,我已经做了:

<?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