Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于值的两个多维数组之间的差异_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 基于值的两个多维数组之间的差异

Php 基于值的两个多维数组之间的差异,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有两个数组 Array ( [0] => Array ( [option_value_id] => 82 [product_option_value_id] => 18771 [quantity] => 25 ) [1] => Array ( [option_value_id] => 46 [product_o

我有两个数组

Array ( [0] => Array ( [option_value_id] => 82 [product_option_value_id] => 18771 [quantity] => 25 ) [1] => Array ( [option_value_id] => 46 [product_option_value_id] => 18776 [quantity] => 5 ) ) 排列 ( [0]=>阵列 ( [选项\u值\u id]=>82 [产品选项值id]=>18771 [数量]=>25 ) [1] =>阵列 ( [选项值id]=>46 [产品选项值id]=>18776 [数量]=>5 ) ) 第二阵列

Array ( [0] => Array ( [option_value_id] => 82 [product_option_value_id] => 18771 [name] => Apricot [quantity] => 30 ) [1] => Array ( [option_value_id] => 51 [product_option_value_id] => 18780 [name] => 2XL [quantity] => 5 ) [2] => Array ( [option_value_id] => 48 [product_option_value_id] => 18778 [name] => L [quantity] => 5 ) [3] => Array ( [option_value_id] => 46 [product_option_value_id] => 18776 [quantity] => 5 ) ) 排列 ( [0]=>阵列 ( [选项\u值\u id]=>82 [产品选项值id]=>18771 [名称]=>杏 [数量]=>30 ) [1] =>阵列 ( [选项值id]=>51 [产品选项值id]=>18780 [名称]=>2XL [数量]=>5 ) [2] =>阵列 ( [选项值id]=>48 [产品选项值id]=>18778 [名称]=>L [数量]=>5 ) [3] =>阵列 ( [选项值id]=>46 [产品选项值id]=>18776 [数量]=>5 ) ) 现在我想知道两个数组之间的区别,不管它的排序如何。如果它们是两个数组之间的差异,则显示true或false,结果应为

Array ( [0] => Array ( [option_value_id] => 82 [product_option_value_id] => 18771 [name] => Apricot [quantity] => 30 ) [1] => Array ( [option_value_id] => 51 [product_option_value_id] => 18780 [name] => 2XL [quantity] => 5 ) [2] => Array ( [option_value_id] => 48 [product_option_value_id] => 18778 [name] => L [quantity] => 5 ) ) 排列 ( [0]=>阵列 ( [选项\u值\u id]=>82 [产品选项值id]=>18771 [名称]=>杏 [数量]=>30 ) [1] =>阵列 ( [选项值id]=>51 [产品选项值id]=>18780 [名称]=>2XL [数量]=>5 ) [2] =>阵列 ( [选项值id]=>48 [产品选项值id]=>18778 [名称]=>L [数量]=>5 ) ) 因为在第一个数组中,索引数量从25更改为30,而其他索引在第一个数组中不存在,

使用
数组_diff()
函数,它将解决您的问题

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_diff($a1,$a2);
print_r($result);
?>


输出:
数组([d]=>黄色)

我在这里做一些假设。如果他们错了,请编辑您的问题以澄清

  • 您的第二个阵列永远不会比第一个阵列小
  • 第一个数组中的所有子数组都将存在于第二个数组中
  • 选项\u值\u id
    值在每个数组中都是唯一的

    <?php
    /* setup arrays */
    $a=array(
        array("option_value_id"=>82,
              "product_option_value_id"=>18771,
              "quantity"=>25),
        array("option_value_id"=>46,
              "product_option_value_id"=>18776,
              "quantity"=>5)
    );
    $b=array(
        array("option_value_id"=>82,
              "product_option_value_id"=>18771,
              "name"=>"Apricot",
              "quantity"=>30),
        array("option_value_id"=>51,
              "product_option_value_id"=>18780,
              "name"=>"2XL",
              "quantity"=>5),
        array("option_value_id"=>48,
              "product_option_value_id"=>18778,
              "name"=>"L",
              "quantity"=>5),
        array("option_value_id"=>46,
              "product_option_value_id"=>18776,
              "quantity"=>5)
    );
    /* Use `option_value_id` as new key for each sub-array in $a & $b */
    /* If you can do this earlier in your code, you can omit this step */
    foreach($a as $i=>$arr){
        $new_a[$arr["option_value_id"]]=$arr;
    }
    foreach($b as $i=>$arr){
        $new_b[$arr["option_value_id"]]=$arr;
    }
    /* Loop through $b and check for duplicates. */
    foreach($new_b as $b_key=>$b_sub){
        if(isset($new_a[$b_key]) && $b_sub===$new_a[$b_key]){   // exists in $a & dupe
            unset($new_b[$b_key]);
            echo "option_value_id: $b_key was identical and was dropped.<br>";
        }else{
            echo "option_value_id: $b_key is new or changed (unique)<br>";
        }
    }
    echo "<br>";
    echo (sizeof($new_b)?"True, changes were made":"False, no changes");
    

  • 我故意避免使用array_unique()和array_diff(),因为有些声明不能信任它们使用===比较。至于foreach循环,我已经读过很多次了,它们的性能优于大多数做同样工作的php函数。如果您担心速度问题,请进行基准测试并做出明智的决定。

    到目前为止,您做了哪些尝试?向我们展示您的代码。一个用户两次问同一个问题可能会导致SO过度膨胀——糟糕的形式。请让我们四处搜索你的代码,这样我们就可以解决你的问题,也很糟糕。@ USS366419如果这个解决方案满足你的要求,请给它一个绿色的记号,这样别人就可以认为这个问题得到了充分的回答。
    option_value_id: 82 is new or changed (unique)
    option_value_id: 51 is new or changed (unique)
    option_value_id: 48 is new or changed (unique)
    option_value_id: 46 was identical and was dropped.
    
    True, changes were made