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.