【问题标题】:PHP - "bidirectional" recursive array_diff_key()PHP - “双向”递归 array_diff_key()
【发布时间】:2015-08-17 16:06:01
【问题描述】:

我想在 2 个多维数组上递归应用 array_diff_key() 并找出它们之间的差异。

函数将是“双向”很重要,所以 foo($a, $b)foo($b, $a) 将产生相同的结果

我尝试了什么?老实说,我尝试了太多东西(包括来自http://php.net/manual/en/function.array-diff-key.php 的示例)并且迷路了。

function superMatch($a1, $a2) {

    foreach($a1 as $k => $v) {
        $r[$k] = is_array($v) ? superMatch($a1[$k], $a2[$k]) : array_diff_key($a1, $a2);
    }

    return $r;
}

输入:

$a = array('a' => 'a', 'b' => 'b', 'c' => array('d' => 'd', 'e' => 'e'));
$a = array('a' => 'a', 'b' => 'b', 'c' => array('t' => 'd', 'e' => 'e'));

预期输出:'t'

有人可以给我一个线索吗?

【问题讨论】:

  • 请从输入、代码的当前输出和预期输出中举一个小例子。
  • 好的,现在我们有了您当前的代码,还可以添加例如Array1: [1,2,3,2], Array 2: [3,2,3,1,3], current output: xy, expected output: z 类似这样的东西。
  • 您是否阅读了右侧“相关”下的所有答案?
  • 数组太大。我不能复制粘贴它。预期结果是与原始数组不同的键值对数组。
  • 是的,我做到了。没什么有趣的。

标签: php arrays recursion


【解决方案1】:

你声明你想要双向工作的键的递归差异,所以给定你的输入:

$a = array('a' => 'a', 'b' => 'b', 'c' => array('d' => 'd', 'e' => 'e'));
$b = array('a' => 'a', 'b' => 'b', 'c' => array('t' => 'd', 'e' => 'e'));

你应该得到输出:

$output = array('c'=>array('d'=>'d','t'=>'d'));
//or
$output = array('t'=>'d','d'=>'d');

以下方法将返回输出的第一个版本。但是,您在问题中说,它应该只输出 t,这没有意义,因为它不可能双向工作(因为密钥 d 也不匹配)。

/** return an array that contains keys that do not match between $array and $compare, checking recursively.
* It's bi-directional so it doesn't matter which param is first
*
* @param $array an array to compare
* @param $compare another array 
*
* @return an array that contains keys that do not match between $array and $compare
*/
function keyMatch($array,$compare){

$output = array();
foreach ($array as $key=>$value){
    if (!array_key_exists($key,$compare)){
        //keys don't match, so add to output array
        $output[$key] = $value;
    } else if (is_array($value)||is_array($compare[$key])){
       //there is a sub array to search, and the keys match in the parent array
        $match = keyMatch($value,$compare[$key]);
        if (count($match)>0){
            //if $match is empty, then there wasn't actually a match to add to $output
            $output[$key] = $match;
        }
    }
}
//Literally just renaiming $array to $compare and $compare to $array
// Why? because I copy-pasted the first foreach loop
$compareCopy = $compare;
$compare = $array;
$array = $compareCopy;
foreach ($array as $key=>$value){
    if (!array_key_exists($key,$compare)){
        $output[$key] = $value;
    } else if (is_array($value)||is_array($compare[$key])){
        $match = keyMatch($value,$compare[$key]);
        if (count($match)>0){
            $output[$key] = $match;
        }
    }
}
return $output;

}
$a = array('a' => 'a', 'b' => 'b', 'c' => array('d' => 'd', 'e' => 'e'));
$b = array('a' => 'a', 'b' => 'b', 'c' => array('t' => 'd', 'e' => 'e'));
print_r(keyMatch($a,$b));

哦,这是您的小型示例输入中的example of it working

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 2017-08-18
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    相关资源
    最近更新 更多