How PHP removes duplicate values for multidimensional arrays

Source: Internet
Author: User

1. Defining functions

function Array_unique_new ($arr) {

$t = Array_map (' serialize ', $arr);//Use the Serialize () method to convert an array to a one-dimensional array in string form

$t = Array_unique ($t);//Remove duplicate values

$new _arr = Array_map (' unserialize ', $t);//Then return the newly formed one-dimensional array to PHP value

return $new _arr;

}

2. Defining arrays

$arr = Array (
Array
' Sup_name ' = ' Shanghai ',
' Cn_name ' = ' ethylacetate ',
' En_name ' + ' ethyl acetate 1 ',
' Pinyin ' = ' Yisuanyizhi ',
' Pro_info ' = ' consumables ',
),
Array
' Sup_name ' = ' 2 ' Shanghai,
' Cn_name ' = ' ethylacetate ',
' En_name ' + ' ethyl acetate 2 ',
' Pinyin ' = ' Yisuanyizhi ',
' Pro_info ' = ' consumables ',
),
Array
' Sup_name ' = ' 4 ' Shanghai,
' Cn_name ' = ' ethylacetate ',
' En_name ' + ' ethyl acetate 1 ',
' Pinyin ' = ' Yisuanyizhi ',
' Pro_info ' = ' consumables ',
),
Array
' Sup_name ' = ' Shanghai ',
' Cn_name ' = ' ethylacetate ',
' En_name ' + ' ethyl acetate 3 ',
' Pinyin ' = ' Yisuanyizhi ',
' Pro_info ' = ' consumables ',
),
Array
' Sup_name ' = ' 1 ' Shanghai,
' Cn_name ' = ' ethylacetate ',
' En_name ' + ' ethyl acetate 4 ',
' Pinyin ' = ' Yisuanyizhi ',
' Pro_info ' = ' consumables ',
),
Array
' Sup_name ' = ' Shanghai ',
' Cn_name ' = ' ethylacetate ',
' En_name ' + ' ethyl acetate 3 ',
' Pinyin ' = ' Yisuanyizhi ',
' Pro_info ' = ' consumables ',
),
Array
' Sup_name ' = ' 1 ' Shanghai,
' Cn_name ' = ' ethylacetate ',
' En_name ' + ' ethyl acetate 4 ',
' Pinyin ' = ' Yisuanyizhi ',
' Pro_info ' = ' consumables ',
),
);

3. Perform the test

$new _arr = $this->array_unique_new ($arr);

Echo ' <pre/> ';

Print_r ($new _arr);

How PHP removes duplicate values for multidimensional arrays

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.