Php multi-dimensional array deduplication

Source: Internet
Author: User
Php multi-dimensional array de-duplication, how to remove the redundant name for?
Array
(
[0] => Array
(
[Id] => 79
[Name] =>
)

[1] => Array
(

[Id] => 79
[Name] =>
)
[2] => Array
(
[Id] => 78
[Name] => B
)
[3] => Array
(

[Id] => 36
[Name] => c
)
)


Reply to discussion (solution)

$ar = 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',  ),);foreach($ar as $k=>$f) {  foreach($ar as $p=>$t) if($k != $p && $f == $t) unset($ar[$k]);}print_r($ar);
Array(    [1] => Array        (            [id] => 79            [name] => a        )    [2] => Array        (            [id] => 78            [name] => b        )    [3] => Array        (            [id] => 36            [name] => c        ))

$arr=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'     ),);$item=array();foreach($arr as $k=>$v){    if(!isset($item[$v['id']])) $item[$v['id']]=$v;}print_r(array_values($item));

Array(    [0] => Array        (            [id] => 79            [name] => a        )    [1] => Array        (            [id] => 78            [name] => b        )    [2] => Array        (            [id] => 36            [name] => c        ))

    array (    'id' => '79',    'name' => 'a',  ),  1 =>   array (    'id' => '79',    'name' => 'a',  ),  2 =>   array (    'id' => '78',    'name' => 'b',  ),  3 =>   array (    'id' => '36',    'name' => 'c',  ),);$temp =array();$result =array();foreach($ar as $item){$temp[] = json_encode($item);}$temp = array_unique($temp);foreach($temp as $item){$result[] = json_decode($item,true);}var_dump($result);

It only provides new ideas with low efficiency.


array(3) {  [0]=>  array(2) {    ["id"]=>    string(2) "79"    ["name"]=>    string(1) "a"  }  [1]=>  array(2) {    ["id"]=>    string(2) "78"    ["name"]=>    string(1) "b"  }  [2]=>  array(2) {    ["id"]=>    string(2) "36"    ["name"]=>    string(1) "c"  }}

$ar = 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',  ),);foreach($ar as $k=>$f) {  foreach($ar as $p=>$t) if($k != $p && $f == $t) unset($ar[$k]);}print_r($ar);
Array(    [1] => Array        (            [id] => 79            [name] => a        )    [2] => Array        (            [id] => 78            [name] => b        )    [3] => Array        (            [id] => 36            [name] => c        ))


If there are only duplicate IDs, how can I remove the duplicate names?

Then you have the #2 code.
But strictly speaking, this is not "de-duplication", but "clustering"
Only one data set in the same group is used.

Then you have the #2 code.
But strictly speaking, this is not "de-duplication", but "clustering"
Only one data set in the same group is used.


Can I directly filter out duplicates during query?

Select distinct ('id') AS 'id', name FROM 'table'

Select distinct ('id') AS 'id', name FROM 'table'


This is my SQL statement. I cannot change it. could you change it for me? thank you.
$ SQL = "select t3.width as width, t3.height as height, t1.add _ time as add_time, t1.add _ author as add_author, t1.image _ url as image_url, t1.id as id, t1.browse _ real_cnt as browse_real_cnt from comments as t2 left join ornamentations as t1 on t1.id = t2.object _ id left join operations as t3 on t3.ornamentation _ id = t1.id where (t2.uid = $ id) AND (t1.enable = '0') order by id desc LIMIT 0, 5 ";

Which of your fields have already exists?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.