如何从PHP中的关联数组中删除重复值?

时间:2017-11-29 07:38:36

标签: php multidimensional-array associative-array

我有从JSON文件转换的数组。此数组中的数据是重复的。如何删除重复数据?

 
 Array
   (
       [0] => Array
           (

               [code] => A
               [children] => Array
                   (
                       [0] => Array
                           (

                               [code] => A-4
                               [children] => Array
                                   (
                                       [0] => Array
                                           (

                                               [code] => A-4-2
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1224
                                               [name] => ALTERNATOR
                                           )

                                       [1] => Array
                                           (

                                               [code] => A-4-3
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1225
                                               [name] => STARTER
                                           )

                                       [2] => Array
                                           (

                                               [code] => A-4-1
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1223
                                               [name] => IGNITION COIL & SPARK PLUG
                                           )

                                       [3] => Array
                                           (

                                               [code] => A-4-4
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1226
                                               [name] => VACUUM PUMP
                                           )

                                       [4] => Array
                                           (

                                               [code] => A-4-2
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1224
                                               [name] => ALTERNATOR
                                           )

                                       [5] => Array
                                           (

                                               [code] => A-4-3
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1225
                                               [name] => STARTER
                                           )

                                       [6] => Array
                                           (

                                               [code] => A-4-1
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1223
                                               [name] => IGNITION COIL & SPARK PLUG
                                           )

                                       [7] => Array
                                           (

                                               [code] => A-4-4
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1226
                                               [name] => VACUUM PUMP
                                           )

                                   )

                               [id] => 1183
                               [name] => Ignition
                           )

                   )

               [id] => 1171
               [name] => Engine/Cooling/Emission/Fuel
           )
       )

输出应该是这样的

 
 Array
   (
       [0] => Array
           (

               [code] => A
               [children] => Array
                   (
                       [0] => Array
                           (

                               [code] => A-4
                               [children] => Array
                                   (
                                       [0] => Array
                                           (

                                               [code] => A-4-2
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1224
                                               [name] => ALTERNATOR
                                           )

                                       [1] => Array
                                           (

                                               [code] => A-4-3
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1225
                                               [name] => STARTER
                                           )

                                       [2] => Array
                                           (

                                               [code] => A-4-1
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1223
                                               [name] => IGNITION COIL & SPARK PLUG
                                           )

                                       [3] => Array
                                           (

                                               [code] => A-4-4
                                               [children] => Array
                                                   (
                                                   )

                                               [id] => 1226
                                               [name] => VACUUM PUMP
                                           )


                                   )

                               [id] => 1183
                               [name] => Ignition
                           )

                   )

               [id] => 1171
               [name] => Engine/Cooling/Emission/Fuel
           )
       )

2 个答案:

答案 0 :(得分:0)

使用 array_unique 解决您的问题:

$array['children'] = array_unique($array['children'], SORT_REGULAR);
// SORT_REGULAR means compare items normally (don't change types)

答案 1 :(得分:0)

我还没有测试过我的代码..试试这个: -

 $array = //put the array in this variable
 foreach($array as $mkey $arr){
     foreach($arr['children'] as $skey => $arrchild){
         $array[$mkey]['children'][$skey]['children']  = array_unique($arrchild['children']);
   }
}
echo "<pre>"; print_r($array); die;

希望它有所帮助!