Heim >Backend-Entwicklung >PHP-Tutorial >php多维数组去重,该如何解决

php多维数组去重,该如何解决

WBOY
WBOYOriginal
2016-06-13 12:09:421417Durchsuche

php多维数组去重
php多维数组去重,怎样去掉name为a的多余的那一个呢?
Array
(
    [0] => Array
        (
            [id] => 79
            [name] => a
        )

    [1] => Array
        (
        
            [id] => 79
            [name] => a
        )
    [2] => Array
        (
            [id] => 78
            [name] => b
        )
    [3] => Array
        (

            [id] => 36
            [name] => c
        )
)
------解决思路----------------------

$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 />}

------解决思路----------------------
SELECT DISTINCT(`id`) AS `id` , name FROM `TABLE`

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn