Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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,我有一个$u的帖子,它发送了一个数组。我有一个前一个数组,其中包含一个键,该键可以包含或不包含来自$\u POST的一个值 例如: $_post: Array ( [0] => 13 [1] => 10 [2] => 52) Previous: Array ( [0] => Array ( [collection_id] => 13 [artwork_id] => 21 ) [1] => Array ( [coll

我有一个$u的帖子,它发送了一个数组。我有一个前一个数组,其中包含一个键,该键可以包含或不包含来自$\u POST的一个值

例如:

$_post: Array ( [0] => 13 [1] => 10 [2] => 52) 
Previous: Array ( [0] => Array ( [collection_id] => 13 [artwork_id] => 21 ) 
                  [1] => Array ( [collection_id] => 11 [artwork_id] => 21 ) ) 
因此,我需要检查previous数组([collection\u id]键)上是否已经存在$\u POST ITM,并提取新的(在本例中为
[1]=>10[2]=>52
)以添加到数据库中,同时获取需要从数据库中删除(替换)的已更改内容

这是我当前的代码,但工作不好

            $new_nodes = array();

            $i = 0;
            foreach($old_nodes as $node){
                foreach ($collections as $collection) {
                    $new = array('collection_id' => $collection, 'artwork_id' => $artwork['id']);
                    if(array_diff($node, $new)){
                        if($collection > 0){
                            array_push($new_nodes, $new);
                        }
                    }
                    else{
                        unset($old_nodes[$i]);
                    }
                }
                $i++;
            }


            foreach($new_nodes as $node){
                for ($i = 0; $i <= count($new_nodes); $i++) {
                    if(isset($new_nodes[$i])){
                        if(!array_diff($node, $new_nodes[$i])){
                            unset($new_nodes[$i]);
                        }
                    }
                }
            }
$new_nodes=array();
$i=0;
foreach($node作为$node的旧_节点){
foreach($collections作为$collection){
$new=array('collection\u id'=>$collection,'artwork\u id'=>$artwork['id']);
if(数组_diff($node,$new)){
如果($collection>0){
数组推送($new\u节点,$new);
}
}
否则{
未设置($old_节点[$i]);
}
}
$i++;
}
foreach($node作为$node的新_节点){

对于($i=0;$i请尝试以下操作:

$old_nodes = array();
$new_nodes = array();
$del_nodes = array();

foreach ($collections as $collection) {
    array_push($old_nodes, $collection['collection_id']);
}

$new_nodes = array_diff($collections, $old_nodes);

$del_nodes = array_diff($old_nodes, $collections);

添加
[1]=>10[2]=>52
后,阵列的外观如何?它们是成为新的子阵列还是添加到其中一个子阵列?