PHP 多次元配列の重複排除
PHP 多次元配列の重複排除、a という名前の冗長なものを削除するにはどうすればよいですか?
配列
(
[0] =>配列
(
[id] => 79
[name] => )
[1] => 配列
(
[id] => 79
[名前] => a
)
[2] => 配列
(
[id] => 78
[name] => b
)
[3] => 配列
(
; ------ ----
-----ソリューションのアイデア----------- --
$ar = array (<br /> 0 => <br /> array (<br /> 'id' => '79',<br /> 'name' => 'a',<br /> ),<br /> 1 => <br /> array (<br /> 'id' => '79',<br /> 'name' => 'a',<br /> ),<br /> 2 => <br /> array (<br /> 'id' => '78',<br /> 'name' => 'b',<br /> ),<br /> 3 => <br /> array (<br /> 'id' => '36',<br /> 'name' => 'c',<br /> ),<br />);<br />foreach($ar as $k=>$f) {<br /> foreach($ar as $p=>$t) if($k != $p && $f == $t) unset($ar[$k]);<br />}<br />print_r($ar);
Array<br />(<br /> [1] => Array<br /> (<br /> [id] => 79<br /> [name] => a<br /> )<br /><br /> [2] => Array<br /> (<br /> [id] => 78<br /> [name] => b<br /> )<br /><br /> [3] => Array<br /> (<br /> [id] => 36<br /> [name] => c<br /> )<br /><br />)<br /><br />
<br />$arr=array(<br /> 0=>array(<br /> 'id'=>79,<br /> 'name'=>'a'<br /> ),<br /> 1=>array(<br /> 'id'=>79,<br /> 'name'=>'a'<br /><br /> ),<br /> 2=>array(<br /> 'id'=>78,<br /> 'name'=>'b'<br /> ),<br /> 3=>array(<br /> 'id'=>36,<br /> 'name'=>'c'<br /> ),<br />);<br />$item=array();<br />foreach($arr as $k=>$v){<br /> if(!isset($item[$v['id']])) $item[$v['id']]=$v;<br />}<br />print_r(array_values($item));<br />
<br />Array<br />(<br /> [0] => Array<br /> (<br /> [id] => 79<br /> [name] => a<br /> )<br /><br /> [1] => Array<br /> (<br /> [id] => 78<br /> [name] => b<br /> )<br /><br /> [2] => Array<br /> (<br /> [id] => 36<br /> [name] => c<br /> )<br /><br />)<br /><br />
<br /><?php<br /><br />$ar = array (<br /> 0 => <br /> array (<br /> 'id' => '79',<br /> 'name' => 'a',<br /> ),<br /> 1 => <br /> array (<br /> 'id' => '79',<br /> 'name' => 'a',<br /> ),<br /> 2 => <br /> array (<br /> 'id' => '78',<br /> 'name' => 'b',<br /> ),<br /> 3 => <br /> array (<br /> 'id' => '36',<br /> 'name' => 'c',<br /> ),<br />);<br /><br /><br />$temp =array();<br />$result =array();<br /><br />foreach($ar as $item){<br /> $temp[] = json_encode($item);<br />}<br /><br />$temp = array_unique($temp);<br /><br />foreach($temp as $item){<br /> $result[] = json_decode($item,true);<br />}<br /><br />var_dump($result);
<br />array(3) {<br /> [0]=><br /> array(2) {<br /> ["id"]=><br /> string(2) "79"<br /> ["name"]=><br /> string(1) "a"<br /> }<br /> [1]=><br /> array(2) {<br /> ["id"]=><br /> string(2) "78"<br /> ["name"]=><br /> string(1) "b"<br /> }<br /> [2]=><br /> array(2) {<br /> ["id"]=><br /> string(2) "36"<br /> ["name"]=><br /> string(1) "c"<br /> }<br />}