从多维数组中删除所有重复值

时间:2016-09-08 21:34:19

标签: php multidimensional-array

我尝试了很多解决方案,但没有一个能解决我的问题。例如,我有3个数组(它们的数量可以从1到4不等),我想删除多维数组中的所有重复值

实施例

   Array
    (
        [33] => Array
            (
                [product_id] => 33
                [name] => Aliquam dolor tellus Aliquam dolor tellus
                [color] => Brown
                [breat] => 3
                [shorten] => Yes
                [material] => Natural
                [rigidity] => Low

        )

    [54] => Array
        (
            [product_id] => 54
            [name] => New Style
            [color] => Black
            [breat] => 4
            [shorten] => Yes
            [material] => Natural
            [rigidity] => Low
)
[23] => Array
            (
                [product_id] => 23
                [name] => New Natural
                [color] => Yellow
                [breat] => 5
                [shorten] => No
                [material] => Natural
                [rigidity] => Low
    )

    )

这是所需的数组。

 Array
(
    [33] => Array
        (
            [product_id] => 33
            [name] => Aliquam dolor tellus Aliquam dolor tellus
            [color] => Brown
            [breat] => 3
            [shorten] => Yes
            [material] =>
            [rigidity] =>

        )

    [54] => Array
        (
            [product_id] => 54
            [name] => New Style
            [color] => Black
            [breat] => 4
            [shorten] => Yes
            [material] =>
            [rigidity] =>
)
[23] => Array
            (
                [product_id] => 23
                [name] => New Natural
                [color] => Yellow
                [breat] => 5
                [shorten] => No
                [material] =>
                [rigidity] =>
    )

    )

2 个答案:

答案 0 :(得分:1)

您可以使用下面注释代码中显示的逻辑删除其值在所有子阵列中完全相同的键。但是,以下“准则”仅关注3个关键字:materialrigidityshorten。如果您想对所有密钥执行此操作,您只需自己修改代码即可。

  

注意:所有所谓的重复项都已删除...它们没有分配 NULL 的值,因为它没有多大意义但你可以如果您愿意,还可以为它们指定 NULL 的值。

    $arr =  [
        33  =>[
            'product_id'=> 33,
            'name'      => 'Aliquam dolor tellus Aliquam dolor tellus',
            'color'     => 'Brown',
            'breat'     => 3,
            'shorten'   => 'Yes',
            'material'  => 'Natural',
            'rigidity'  => 'Low',
        ],
        54  =>[
            'product_id'=> 54,
            'name'      => 'New Style',
            'color'     => 'Black',
            'breat'     => 4,
            'shorten'   => 'Yes',
            'material'  => 'Natural',
            'rigidity'  => 'Low',
        ],
        23  =>[
            'product_id'=> 23,
            'name'      => 'New Natural',
            'color'     => 'Yellow',
            'breat'     => 5,
            'shorten'   => 'No',
            'material'  => 'Natural',
            'rigidity'  => 'Low',
        ],
    ];

    // CREATE TEMPORAL ARRAYS TO HOLD SCALAR VALUES CORRESPONDING TO 
    // THE KEYS WHOSE DUPLICITY YOU WISH TO CHECK.
    // YOU CAN DO THIS FOR ALL THE KEYS IF YOU CHOOSE
    // HERE WE LIMIT THOSE TO THE 3 KEYS BELOW:
    $material = $rigidity = $shorten = [];


    // LOOP THROUGH YOUR ARRAY AND BUNDLE THE VALUES FOR THE 
    // `rigidity`, `shorten` & `material` KEYS INTO UNIQUE ARRAYS 
    // TO BE USED LATER FOR FURTHER PROCESSING *YOUR* DUPLICATES
    foreach($arr as $pid=>$data){
        // ADD THE VALUES OF THE `rigidity`, `shorten` AND `material` KEYS
        // TO THE $rigidity, $shorten AND $material ARRAY RESPECTIVELY:
        $material[] = $data['material'];
        $rigidity[] = $data['rigidity'];
        $shorten[]  = $data['shorten'];
    }

    // EXTRACT THE UNIQUE VALUES OF THE $material, $shorten AND $rigidity ARRAYS
    // THE LENGTHS OF WHICH WE SHALL USE TO DETERMINE IF THEY ARE SAME OR NOT
    // IF THE LENGTH IS 1; THEN THEY ARE THE SAME ACROSS ALL SUB-ARRAY
    // AND THUS ARE FIT TO BE DELETED...
    $material   = array_unique($material);
    $rigidity   = array_unique($rigidity);
    $shorten    = array_unique($shorten);


    // LOOP AGAIN THROUGH YOUR ARRAY BUT THIS TIME
    // ONLY TO DELETE THE KEYS THAT ARE THE SAME ACROSS ALL SUB-ARRAY
    foreach($arr as $pid=>&$data){
        if(count($rigidity) == 1){
            unset($data['rigidity']);
        }
        if(count($material) == 1){
            unset($data['material']);
        }
        if(count($shorten) == 1){
            unset($data['shorten']);
        }
    }
    // CHECK OUT THE CURRENT VALUE OF THE ARRAY YOU STARTED WITH
    var_dump($arr);

THE var_dump($ arr)ABOVE YIELDS:

    array (size=3)
      33 => 
        array (size=5)
          'product_id' => int 33
          'name' => string 'Aliquam dolor tellus Aliquam dolor tellus' (length=41)
          'color' => string 'Brown' (length=5)
          'breat' => int 3
          'shorten' => string 'Yes' (length=3)
      54 => 
        array (size=5)
          'product_id' => int 54
          'name' => string 'New Style' (length=9)
          'color' => string 'Black' (length=5)
          'breat' => int 4
          'shorten' => string 'Yes' (length=3)
      23 => 
        array (size=5)
          'product_id' => int 23
          'name' => string 'New Natural' (length=11)
          'color' => string 'Yellow' (length=6)
          'breat' => int 5
          'shorten' => string 'No' (length=2)

答案 1 :(得分:1)

您可以使用多个阵列。但是你必须经过两次。像这样......

$array = //your array here

//hold the duplicates and values that exist to a name 
$dupes = $exists = Array();

//get the duplicates
foreach($array as $nextArray)
{
    foreach($nextArray as $key => $val)
    {
        if(!isset($counts[$key])) 
            $counts[$key] = Array($val);
        else {
            //since the key already exists check to see if the value exists
            if(in_array($val,$counts[$key])) {
                if(!isset($dupes[$key]))
                    $dupes[$key] = array($val); 
            }
            else {
                $counts[$key][] = $val;
            }
        }           
    }
}

//apply the duplicates to the array
foreach($array as $arrKey => $nextArray)
{
    foreach($nextArray as $key => $val)
    {
        //if a dupe exist then make the value an empty string
        if(isset($dupes[$key]) && in_array($val,$dupes[$key]))
            $array[$arrKey][$key] = ''; 
    }   
}
相关问题