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



我尝试了很多解决方案,但没有一个适合我的情况。例如,我有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] =>
    )
    )

您可以使用下面注释代码中提供的逻辑来删除键,其值在所有子数组中完全相同。然而,下面的代码只关注3个键,即:material, rigidityshorten。如果您想对所有的键都这样做,您可以简单地修改代码,以达到这种效果。

注:所有所谓的duplicate are Deleted…它们没有被赋值为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);

var_dump (arr)美元以上收益率:

    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)

可以使用多个数组。但是你需要循环两次。像这样…

$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] = ''; 
    }   
}

相关内容

  • 没有找到相关文章

最新更新