PHP two-dimensional array deduplication-php Tutorial

Source: Internet
Author: User
PHP two-dimensional array deduplication $ array = array (
'1' => array ('title' => '20140901', 'date' => '20140901 '),
'2' => array ('title' => '123', 'date' => '123 '),
'3' => array ('title' => '123', 'date' => '123 ')
);


You want to write a function, input an array, and delete duplicate values,
For example, the 'title' => '123' of $ array ['2'] is also found in $ array ['1']. delete $ array ['2'], $ array ['3'] is $ array ['2']


Reply to discussion (solution)

$array = array('1'=>array('title'=>'1111','date'=>'2222'),'2'=>array('title'=>'1111','date'=>'5555'),'3'=>array('title'=>'4444','date'=>'3333'));$tmp = array();foreach($array as $v)  if(!isset($tmp[$v['title']])) $tmp[$v['title']] = $v;$array = array_values($tmp);print_r($array);
Array(    [0] => Array        (            [title] => 1111            [date] => 2222        )    [1] => Array        (            [title] => 4444            [date] => 3333        ))

$array = array('1'=>array('title'=>'1111','date'=>'2222'),'2'=>array('title'=>'1111','date'=>'5555'),'3'=>array('title'=>'4444','date'=>'3333'));$tmp = array();foreach($array as $v)  if(!isset($tmp[$v['title']])) $tmp[$v['title']] = $v;$array = array_values($tmp);print_r($array);
Array(    [0] => Array        (            [title] => 1111            [date] => 2222        )    [1] => Array        (            [title] => 4444            [date] => 3333        ))


This method is simple, but it is also a bit flawed. one is to get rid of the original index, and the other is to use additional variables to store arrays to occupy more memory, which may be affected when the array is large.

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.