如何根据某个值拆分数组?

时间:2012-02-20 00:13:26

标签: php arrays multidimensional-array

我目前有一系列基于位置的信息,类似于下面的信息(已按时间排序)。第一个键/值是表示旅程开始(00),跑步旅程(01)和旅程结束(10)的事件。

Array ( 
[0] => Array ( [event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[1] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[2] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[3] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[4] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[5] => Array ( [event] => 10 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[6] => Array ( [event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[7] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[8] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[9] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[10] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[11] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[12] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[13] => Array ( [event] => 10 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[14] => Array ( [event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[15] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[16] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[17] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[18] => Array ( [event] => 10 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
)

如何进一步拆分数组,以便为​​每个旅程创建一个新数组,以便我最终得到这个:

Array (
[0] => Array (
[0] => Array ( [event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[1] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[2] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[3] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[4] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[5] => Array ( [event] => 10 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
)
[1] => Array (
[0] => Array ( [event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[1] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[2] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[3] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[4] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[5] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[6] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[7] => Array ( [event] => 10 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
)
...
)

我还没有尝试过任何东西(除了谷歌搜索),因为我真的不知道从哪里开始。

2 个答案:

答案 0 :(得分:2)

你需要这样的自定义解决方案:

 $outputArray = array(); $currentArray = array();
 foreach($inputArray as $key=>$subArray) {
   if($subArray['event']==00) {
      $outputArray[] = $currentArray;
      $currentArray = array($subArray);
   } else {
      $currentArray[] = $subArray;
   }
 }
 $outputArray[] = $currentArray;
 array_shift($outputArray);

输入$ inputArray然后输出$ outputArray。享受!

答案 1 :(得分:1)

试试这个:

$journey = array ( 
[0] => Array ( [event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[1] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[2] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[3] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[4] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[5] => Array ( [event] => 10 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[6] => Array ( [event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[7] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[8] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[9] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[10] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[11] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[12] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[13] => Array ( [event] => 10 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[14] => Array ( [event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[15] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[16] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[17] => Array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
[18] => Array ( [event] => 10 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 )
)

$journeygrouping = array();

while(!empty($journey)){
$temp = array_pop($journey);

if(isset($journeygrouping[$temp['event']])){
  array_push($journeygrouping[$temp['event']], $temp);
} else {
  $journeygrouping[$temp['event']] = array();
  array_push($journeygrouping[$temp['event']], $temp);
}
}

此代码应创建一个数组结构,如:

array(
[00] => array([0] => array([event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111) [1]=>array ( [event] => 00 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111 ))
[01] => array([0]=>array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111) [1]=>array ( [event] => 01 [time] => 1329293878 [lat] => 66.66666 [lon] => 11.11111)  )
);

依旧......