日期:2014-05-17 浏览次数:20799 次
$ar = array( 0 =>array( 'name' => "a", 'time' => '879798'), 1 => array( 'name' => "a9", 'time' => '856565'), //85665565 ? 示例数据有问题吧,看不出是按time排序的! 2 =>array( 'name' => "hdhdjd", 'time' => '815465'), 3 =>array( 'name' => "a", 'time' => '800000'), 4 =>array( 'name' => "dr", 'time' => '789898'), ); $result = array(); $len = count($ar); for ($i = 0; $i < $len; $i ++) { $result[] = array_shift($ar); for ($j = 0; $j < count($ar); $j ++) { if ($ar[$j]['name'] == $result[count($result) - 1]['name']) { $result[] = array_splice($ar, $j, 1); break; } } } echo '<pre>'; print_r($result); /* Array ( [0] => Array ( [name] => a [time] => 879798 ) [1] => Array ( [0] => Array ( [name] => a [time] => 800000 ) ) [2] => Array ( [name] => a9 [time] => 856565 ) [3] => Array ( [name] => hdhdjd [time] => 815465 ) [4] => Array ( [name] => dr [time] => 789898 ) [5] => ) */
------解决方案--------------------
有点问题,修改一下:
$result = array(); $len = count($ar); for ($i = 0; $i < $len - 1; $i ++) { $result[] = array_shift($ar); for ($j = 0; $j < count($ar) - 1; $j ++) { if ($ar[$j]['name'] == $result[count($result) - 1]['name']) { $item = array_splice($ar, $j, 1); $result[] = $item[0]; break; } } }
------解决方案--------------------
<?php $arr = array( array( 'name' => "a", 'time' => '879798'), array( 'name' => "a9", 'time' => '85665565'), array( 'name' => "dr", 'time' => '8154465'), array( 'name' => "a", 'time' => '800000'), array( 'name' => "dr", 'time' => '789898'), ); $names = array(); $index = array(); array_walk($arr, function($val, $key){ global $names;