Home >Backend Development >PHP Tutorial >多次foreach循环结果去重
$a=array(0 =>array([logo] => Array
(
[id] =>1
[sid] => 6
)
1=>array([logo] => Array
(
[id] =>2
[sid] => 6
)
)
使用foreach循环$a,只显示不同的SID;
贴出经var_export() 运行后的数组出来
$last = 0;foreach($a as $r) { foreach($r as $v) { if($last != $v['sid']) echo $v['sid']; $last = $v['sid']; }}
array ( 0 => array ( 'logo' => array ( 'id' => 1, 'sid' => 6, ), ), 1 => array ( 'logo' => array ( 'id' => 2, 'sid' => 6, ), ), )
$res = array();foreach($a as $v){ if(!isset($res[$v['logo']['sid']])) $res[$v['logo']['sid']] = $v;}print_r(array_values($res));
$last = 0;foreach($a as $r) { foreach($r as $v) { if($last != $v['sid']) echo $v['sid']; $last = $v['sid']; }}
$res = array();foreach($a as $v){ if(!isset($res[$v['logo']['sid']])) $res[$v['logo']['sid']] = $v;}print_r(array_values($res));