Php removes the repeated items of two-dimensional arrays, php removes two-dimensional arrays _ PHP Tutorial

Source: Internet
Author: User
Php removes the repeated items of two-dimensional arrays, and php removes two-dimensional arrays. Php removes duplicate items from two-dimensional arrays. php removes duplicate items from two-dimensional arrays in php. you can use the php built-in function array_unique (), however, the array_unique function of php removes the repeated items of the two-dimensional array from the multi-dimensional php, and php removes the two-dimensional array.

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.

Repeated items for removing one-dimensional arrays in php can be implemented using the php built-in function array_unique (), but the php array_unique function is used for multi-dimensional data...

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.