Php deduplication method for two-dimensional arrays

Source: Internet
Author: User
This article mainly introduces php's method for removing duplicate items from two-dimensional arrays. For more information, see

This article mainly introduces php's method for removing duplicate items from two-dimensional arrays. For more information, see

The php built-in function array_unique () can be used to remove repeated items from one-dimensional arrays. However, the php array_unique function is not applicable to multi-dimensional arrays. How can we remove repeated items from two-dimensional arrays?

The following is a function.

// Remove the duplicate value from the two-dimensional array function unique_arr ($ array2D, $ stkeep = false, $ ndformat = true) {$ joinstr = '++ '; // determine whether the primary array key is retained (the primary array key can be non-numeric) if ($ stkeep) $ stArr = array_keys ($ array2D ); // determine whether the secondary array key is retained (all secondary array keys must be the same) if ($ ndformat) $ ndArr = array_keys (end ($ array2D); // dimension reduction, you can also use implode to convert a one-dimensional array to a comma-connected string foreach ($ array2D as $ v) {$ v = join ($ joinstr, $ v ); $ temp [] = $ v;} // remove the duplicate string, that is, the repeated one-dimensional array $ temp = array_unique ($ temp ); // re-assemble the split array foreach ($ temp as $ k => $ v) {if ($ stkeep) $ k = $ stArr [$ k]; if ($ ndformat) {$ tempArr = explode ($ joinstr, $ v); foreach ($ tempArr as $ ndkey => $ ndval) $ output [$ k] [$ ndArr [$ ndkey] = $ ndval;} else $ output [$ k] = explode ($ joinstr, $ v );} return $ output ;}

I hope it will be helpful for you to learn about php programming.

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.