Php array deduplication

Source: Internet
Author: User
: This article mainly introduces php array deduplication. For more information about PHP tutorials, see. One-dimensional array

$aa=array("apple","banana","pear","apple","wail","watermalon"); $bb=array_unique($aa); print_r($bb);//Array ( [0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon

Two-dimensional array

1) because the values of one-click names cannot be repeated, duplicate items are deleted.

$ Aa = array ('id' => 123, 'name' => 'Zhang San'), array ('id' => 123, 'name' => 'Li si'), array ('id' => 124, 'name' => 'Wang 5'), array ('id' => 125, 'name' => 'Zhao Liu'), array ('id' => 126, 'name' => 'Zhao Liu'); // requirement, id cannot be repeated functionassoc_unique ($ arr, $ key) {$ tmp_arr = array (); foreach ($ arras $ k => $ v) {// search for $ v [$ key] in the $ tmp_arr array. If yes, return trueif (in_array ($ v [$ key], $ tmp_arr )) {unset ($ arr [$ k]);} else {$ tmp_arr [] = $ v [$ key] ;}} sort ($ arr ); // the sort function sorts the array. return $ arr;} $ key = 'id'; assoc_unique (& $ aa, $ key); print_r ($ aa ); // Array ([0] => Array ([id] => 123 [name] => Zhang San) [1] => Array ([id] => 124 [name] => Wang Wu) [2] => Array ([id] => 125 [name] => Zhao 6) [3] => Array ([id] => 126 [name] => Zhao 6 ))

2) because the internal one-dimensional arrays cannot be completely the same, the duplicate items are deleted.

$ Aa = array ('id' => 123, 'name' => 'Zhang San'), array ('id' => 123, 'name' => 'Li si'), array ('id' => 124, 'name' => 'Wang 5'), array ('id' => 123, 'name' => 'Li si'), array ('id' => 126, 'name' => 'Zhao Liu'); functionarray_unique_fb ($ array2D) {foreach ($ array2Das $ v) {$ v = join (",", $ v); // dimensionality reduction, you can also use implode, convert a one-dimensional array to a comma-connected string $ temp [] = $ v;} $ temp = array_unique ($ temp); // remove the duplicate string, that is, the repeated one-dimensional array foreach ($ tempas $ k => $ v) {$ temp [$ k] = explode (",", $ v ); // re-assemble the split array} return $ temp;} $ bb = array_unique_fb ($ aa); print_r ($ bb ); // Array ([0] => Array ([0] => 123 [1] => John 3) [1] => Array ([0] => 123 [1] => Li Si) [2] => Array ([0] => 124 [1] => Wang Wu) [4] => Array ([0] => 126 [1] => Zhao 6 ))

Copyright disclaimer: knowledge is used by the people! You are welcome to repost this article. For more information, see the link at the beginning!

The above introduces php array de-duplication, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.