How to remove duplicate values in php Multidimensional Array
This article is about how php multidimensional arrays remove duplicate values. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Php multi-dimensional array to remove duplicate values: 1, the use of "array_map ('serialize', $arr)" to convert the multi-dimensional array into a string in the form of an one-dimensional array; 2, use array_unique () to remove repeated values in the one-dimensional array; 3, the deduplicated one-dimensional array back to a multi-dimensional array.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Php multi-dimensional array to remove duplicate values
1. Realize the function of removing repeated values from multi-dimensional array.
Function array_unique_new ($arr) {$t = array_map ('serialize', $arr); / / use the serialize () method to convert the array into an one-dimensional array in the form of a string $t = array_unique ($t); / / remove the duplicate value $new_arr = array_map (' unserialize', $t); / / then convert the newly constructed one-dimensional array back to the php value return $new_arr }
2. An array that needs to remove duplicate values
Arr = array (array ('sup_name' = >' Shanghai', 'cn_name' = >' EthylAcetate', 'en_name' = >' ethyl acetate 1', 'pinyin' = >' yisuanyizhi', 'pro_info' = >' consumables',), array ('sup_name' = >' Shanghai 2', 'cn_name' = >' EthylAcetate', 'en_name' = >' ethyl acetate 2', 'pinyin' = >' yisuanyizhi' 'pro_info' = >' consumables',), array ('sup_name' = >' Shanghai 418', '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' = >' Shanghai', 'cn_name' = >' EthylAcetate', 'en_name' = >' ethyl acetate', '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' = >' Shanghai 1', 'cn_name' = >' EthylAcetate', 'en_name' = >' ethyl acetate 4', 'pinyin' = >' yisuanyizhi', 'pro_info' = >' consumables',))
Look at the output:
3. Execute the test
$new_arr = array_unique_new ($arr); echo''; print_r ($new_arr)
Thank you for reading! This is the end of this article on "how to remove duplicate values in php multidimensional arrays". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!