通过另一个 2d 关联数组的键过滤 2d 关联数组

问题描述 投票:0回答:14

我有两个多维数组,我想要差异。例如。下面我取了二维两个数组

$array1 = Array (
       [a1] => Array  (
          [a_name] => aaaaa
          [a_value] => aaa
     )

       [b1] => Array (
          [b_name] => bbbbb
          [b_value] => bbb
   )
       [c1] => Array (
          [c_name] => ccccc
          [c_value] => ccc
   )

)

$array2 = Array (
 [b1] => Array (
       [b_name]=> zzzzz
     )
)

现在我想要这两个数组的关键区别。我尝试过 array_diff_key() 但它不适用于多维。

array_diff_key($array1, $array2)

我想要的输出如下

//output
$array1 = Array (
   [a1] => Array  (
      [a_name] => aaaaa
      [a_value] => aaa
 )

   [b1] => Array (          
      [b_value] => bbb
)
   [c1] => Array (
      [c_name] => ccccc
      [c_value] => ccc
)

)

如果您认为我的问题是真实的,请接受并回答。谢谢。

编辑

现在如果第二个数组是

$array2 = Array( [b1] => zzzzz)

结果应该是

$array1 = Array (
   [a1] => Array  (
      [a_name] => aaaaa
      [a_value] => aaa
    )     

   [c1] => Array (
      [c_name] => ccccc
      [c_value] => ccc
     )

)
php arrays multidimensional-array filter array-difference
14个回答
21
投票

请检查我是否正确理解您的意思,然后此代码片段可以帮助您解决您的问题。我仅针对您指定的问题对其进行了测试。如果您想运行其他测试用例,您可以告诉我调整代码。

$a1 = array(
    'a1' => array('a_name' => 'aaa', 'a_value' => 'aaaaa'),
    'b1' => array('b_name' => 'bbb', 'b_value' => 'bbbbbb'),
    'c1' => array('c_name' => 'ccc', 'c_value' => 'cccccc')
);

$a2 = array(
    'b1' => array('b_name' => 'zzzzz'),
);

$result = check_diff_multi($a1, $a2);
print '<pre>';
print_r($result);
print '</pre>';


function check_diff_multi($array1, $array2){
    $result = array();
    foreach($array1 as $key => $val) {
         if(isset($array2[$key])){
           if(is_array($val) && $array2[$key]){
               $result[$key] = check_diff_multi($val, $array2[$key]);
           }
       } else {
           $result[$key] = $val;
       }
    }

    return $result;
}

编辑:添加了代码调整。


14
投票

这个解决方案对我非常有帮助,我希望可以帮助他们,无论数组是否混乱。

function your_array_diff($arraya, $arrayb) {

    foreach ($arraya as $keya => $valuea) {
        if (in_array($valuea, $arrayb)) {
            unset($arraya[$keya]);
        }
    }
    return $arraya;
}

$a1 = Array
    (
    "0" => Array
        (
        "Empresa" => "TMC01",
        "Paga" => "13/01/2015",
        "ID" => "M2",
        "Valor" => "200",
        "Nombre" => "BONI"
    ),

    "1" => Array
        (
        "Empresa" => "TMC01",
        "Paga" => "13/01/2015",
        "ID" => "M1",
        "Valor" => "100",
        "Nombre" => "SUELDO"
    )
);

$b1 = Array
    (
    "0" => Array
        (
        "Empresa" => "TMC01",
        "Paga" => "13/01/2015",
        "ID" => "M1",
        "Valor" => "100",
        "Nombre" => "SUELDO"
    ),
    "1" => Array
        (
        "Empresa" => "TMC01",
        "Paga" => "13/01/2015",
        "ID" => "M2",
        "Valor" => "200",
        "Nombre" => "BONI"
    )
);

$resultado = your_array_diff($a1, $b1);

echo "<pre>";
echo print_r($resultado);
echo "</pre>";

12
投票

有很多情况下,原来的答案无法正常工作,所以我写了一个更好的解决方案。问题之一是,如果您删除了数组 2 中的某个属性,其他函数将无法识别它。

function check_diff_multi($array1, $array2){
    $result = array();

    foreach($array1 as $key => $val) {
        if(is_array($val) && isset($array2[$key])) {
            $tmp = check_diff_multi($val, $array2[$key]);
            if($tmp) {
                $result[$key] = $tmp;
            }
        }
        elseif(!isset($array2[$key])) {
            $result[$key] = null;
        }
        elseif($val !== $array2[$key]) {
            $result[$key] = $array2[$key];
        }

        if(isset($array2[$key])) {
            unset($array2[$key]);
        }
    }

    $result = array_merge($result, $array2);

    return $result;
}

我还添加了测试用例来检查结果:

如您所见,我的功能提供了更好的结果。


4
投票
$out = array_diff_assoc_recursive($array1, $array2);

该解决方案需要递归数组值,这些值本身可能就是数组。

function array_diff_assoc_recursive($array1, $array2)
{
    foreach($array1 as $key => $value)
    {
        if(is_array($value))
        {
            if(!isset($array2[$key]))
            {
                $difference[$key] = $value;
            }
            elseif(!is_array($array2[$key]))
            {
                $difference[$key] = $value;
            }
            else
            {
                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                if($new_diff != FALSE)
                {
                    $difference[$key] = $new_diff;
                }
            }
        }
        elseif(!isset($array2[$key]) || $array2[$key] != $value)
        {
            $difference[$key] = $value;
        }
    }
    return !isset($difference) ? 0 : $difference;
}

3
投票

几乎是 @bernhardh 答案的副本,但发布在这里,因为我的编辑被拒绝了。使用 + 而不是 array_merge,因为 array_merge 会重新索引数组,从而导致索引数组出现问题。

/**
 * Given 2 arrays see what has changed when comparing defaults to the new values.
 *
 * @param array $defaults
 *   Array of default values.
  * @param mixed $new_values
 *   Array of new values.
 *
 * @return array
 *   Nested array strucutre; only the diff.
 */
function array_diff_multi(array $defaults, $new_values) {
  $result = array();

  foreach ($defaults as $key => $val) {
    if (is_array($val) && isset($new_values[$key])) {
      $tmp = array_diff_multi($val, $new_values[$key]);
      if ($tmp) {
        $result[$key] = $tmp;
      }
    }
    elseif (!isset($new_values[$key])) {
      $result[$key] = NULL;
    }
    elseif ($val != $new_values[$key]) {
      $result[$key] = $new_values[$key];
    }
    if (isset($new_values[$key])) {
      unset($new_values[$key]);
    }
  }

  $result = $result + $new_values;
  return $result;
}

2
投票

我知道这个线程有点旧,但是我在原始解决方案中遇到了一些问题。所以这是我的问题解决方案。

private function array_diff_recursive($array1, $array2){
    $result = [];
    foreach($array1 as $key => $val) {
        if(array_key_exists($key, $array2)){
            if(is_array($val) || is_array($array2[$key])) {
                if (false === is_array($val) || false === is_array($array2[$key])) {
                    $result[$key] = $val;
                } else {
                    $result[$key] = $this->array_diff_recursive($val, $array2[$key]);
                    if (sizeof($result[$key]) === 0) {
                        unset($result[$key]);
                    }
                }
            }
        } else {
            $result[$key] = $val;
        }
    }
    return $result;
}

遇到/已修复的问题

  1. 结果填充了没有区别的键
  2. 如果一个值是数组而另一个值不是,则它不认为有区别

1
投票

尝试该功能:

<?php
$input = ['blue'  => 1, 'white' => ['purple' => 4, 'green' => 3], 'red' => 2];
$filter = ['blue' => 6, 'white' => ['yellow' => 7, 'green' => 5], 'red' => 2];

/**
 * @param array $input
 * @param array $filter
 * @return array
 */
function multidimensionalArrayDiffKey(array $input, array $filter)
{
    if ($diff = array_diff_key($input, $filter)){
        return $diff;
    }else{
        foreach($input as $key => $value){
            if(is_array($value) && $diff = multidimensionalArrayDiffKey($value, $filter[$key])){
                return [$key => $diff];
            }
        }
    }
    return [];
}
print_r(multidimensionalArrayDiffKey($input, $filter));

结果:

Array
(
    [white] => Array
        (
            [purple] => 4
        )

)

1
投票

一个更好的函数,其工作方式与原始 array_diff 类似。
将 array1 与一个或多个其他数组进行比较,并递归返回 array1 中不存在于任何其他数组中的值。

    <?php

    function md_array_diff(array $array1, array $array2, array $_ = null) {
        $diff = [];
        $args = array_slice(func_get_args(), 1);

        foreach ($array1 as $key => $value) {
            foreach ($args as $item) {
                if (is_array($item)) {
                    if (array_key_exists($key, $item)) {
                        if (is_array($value) && is_array($item[$key])) {
                            $tmpDiff = md_array_diff($value, $item[$key]);

                            if (!empty($tmpDiff)) {
                                foreach ($tmpDiff as $tmpKey => $tmpValue) {
                                    if (isset($item[$key][$tmpKey])) {
                                        if (is_array($value[$tmpKey]) && is_array($item[$key][$tmpKey])) {
                                            $newDiff = array_diff($value[$tmpKey], $item[$key][$tmpKey]);
                                        } else if ($value[$tmpKey] !== $item[$key][$tmpKey]) {
                                            $newDiff = $value[$tmpKey];
                                        }

                                        if (isset($newDiff)) {
                                            $diff[$key][$tmpKey] = $newDiff;
                                        }
                                    } else {
                                        $diff[$key][$tmpKey] = $tmpDiff;
                                    }
                                }
                            }
                        } else if ($value !== $item[$key]) {
                            $diff[$key] = $value;

                        }
                    } else {
                        $diff[$key] = $value;
                    }
                }
            }
        }

        return $diff;
    }

    $arr1 = [
      "A" => [
        "A1" => ['A1-0', 'A1-1', 'A1-2', 'A1-3'],
        "A2" => ['A2-0', 'A2-1', 'A2-2', 'A2-3'],
        "A3" => ['A3-0', 'A3-1', 'A3-2', 'A3-3']
      ],
      "B" => [
        "B1" => ['B1-0', 'B1-1', 'B1-2', 'B1-3'],
        "B2" => ['B2-0', 'B2-1', 'B2-2', 'B2-3'],
        "B3" => ['B3-0', 'B3-1', 'B3-2', 'B3-3']
      ],
      'C' => 123
    ];

    $arr2 = [
      "A" => [
        "A1" => ['A1-1', 'A1-2', 'A1-3'],
        "A2" => ['A2-0', 'A2-1', 'A2-2', 'A2-3'],
        "A3" => ['A3-0', 'A3-1', 'A3-2']
      ],
      "B" => [
        "B1" => ['B1-0', 'B1-2', 'B1-3'],
        "B2" => ['B2-0', 'B2-1', 'B2-2', 'B2-3'],
        "B3" => ['B3-0', 'B3-1', 'B3-3']
      ]
    ];

    $arr3 = [
      "A" => [
        "A1" => ['A1-0', 'A1-1', 'A1-2', 'A1-3'],
        "A2" => ['A2-0', 'A2-1', 'A2-2', 'A2-3'],
        "A3" => ['A3-0', 'A3-1', 'A3-2']
      ],
      "B" => [
        "B1" => ['B1-0', 'B1-2', 'B1-3'],
        "B2" => ['B2-0', 'B2-1', 'B2-2', 'B2-3'],
        "B3" => ['B3-0', 'B3-1', 'B3-3']
      ]
    ];

    $diff = md_array_diff($arr1, $arr2, $arr3);

    ?>
    Will Output:
    array (size=3)
      'A' =>
          array (size=2)
            'A1' =>
              array (size=1)
                0 => string 'A1-0' (length=4)
            'A3' =>
              array (size=1)
                3 => string 'A3-3' (length=4)
      'B' =>
          array (size=2)
            'B1' =>
              array (size=1)
                1 => string 'B1-1' (length=4)
            'B3' =>
              array (size=1)
                2 => string 'B3-2' (length=4)
      'C' => int 123

1
投票

最好检查第二个数组中是否有整个数组可用

        function array_diff_assoc_recursive($arraya, $arrayb) {
            foreach ($arraya as $keya => $valuea) {
                if (in_array($valuea, $arrayb)) {
                    unset($arraya[$keya]);
                }
            }
            return $arraya;
        }

这可能是识别差异最简单、快速且最短的方法!


0
投票

对@Zaheer Abbass 解决方案进行一个小调整,我得到了我想要的结果。非常感谢扎希尔。这是我使用的最终代码。

function check_diff_multi($array1, $array2){
    $result = array();
    foreach($array1 as $key => $val) {
        if(isset($array2[$key])){
           if(is_array($val)  && is_array($array2[$key])){
                $result[$key] = check_diff_multi($val, $array2[$key]);
            }
        } else {
            $result[$key] = $val;
        }
    }

    return $result;
} 

0
投票

因此,如果您有包含空值或空数组的数组。

private function check_diff_multi($array1, $array2){
    $result = array();
    foreach($array1 as $key => $val) {
        if(array_key_exists($key,$array2)){
            if(is_array($val) && is_array($array2[$key]) && !empty($val)){
                $result[$key] = $this->check_diff_multi($val, $array2[$key]);
            }
        } else {
            $result[$key] = $val;
        }
    }
    return $result;
}

0
投票

看来您最多需要一个 foreach 循环来访问过滤器数组中的第一级键,然后根据过滤器数组是否有第二级,您可以与

array_diff_key()
进行另一轮比较或直接
unset()
第一级的排位数据

代码:(演示

$a1 = array(
    'a1' => array('a_name' => 'aaa', 'a_value' => 'aaaaa'),
    'b1' => array('b_name' => 'bbb', 'b_value' => 'bbbbbb'),
    'c1' => array('c_name' => 'ccc', 'c_value' => 'cccccc')
);

function twoLevelFilter($master, $filter) {
    foreach ($filter as $key => $value) {
        if (isset($master[$key])) {
            if (is_iterable($value)) {
                $master[$key] = array_diff_key($master[$key], $value);
            } else {
                unset($master[$key]);
            }
        }
    }
    return $master;
}

var_export(twoLevelFilter($a1, ['b1' => ['b_name' => 'zzzzz']]));
echo "\n---\n";
var_export(twoLevelFilter($a1, ['b1' => 'zzzzz']));

输出:

array (
  'a1' => 
  array (
    'a_name' => 'aaa',
    'a_value' => 'aaaaa',
  ),
  'b1' => 
  array (
    'b_value' => 'bbbbbb',
  ),
  'c1' => 
  array (
    'c_name' => 'ccc',
    'c_value' => 'cccccc',
  ),
)
---
array (
  'a1' => 
  array (
    'a_name' => 'aaa',
    'a_value' => 'aaaaa',
  ),
  'c1' => 
  array (
    'c_name' => 'ccc',
    'c_value' => 'cccccc',
  ),
)

0
投票

function sort($value){
    foreach($value as $i =>  $v){
        if(is_array($v))
            $value[$i] = sort($v);
    }
    
    ksort($value);
    return $value;
}

function isDifferent(array $array1, array $array2){
    return json_encode(sort($array1), true) != json_encode(sort($array2), true);
}

$arr1 = ["a" => "A", "b" => "B", "c" => ["d" => "D"]];
$arr2 = ["a" => "A", "b" => "B", "c" => ["x" => "X"]];

if(isDifferent($arr1, $arr2)){
    echo "They are different";
}else{
    echo "Both arrays are equal";
}

-2
投票

print_r(array_diff_key($array1,$array2));

© www.soinside.com 2019 - 2024. All rights reserved.