将多维数组与简单数组进行比较并提取差异

发布于 2024-10-09 17:38:11 字数 1547 浏览 0 评论 0原文

我有一个发送数组的 $_POST 。我有一个先前的数组,其中包含一个键,该键可能包含或不包含 $_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 ) ) 

所以我需要检查 $_POST itms 是否已存在于先前的数组([collection_id] 键)上并提取新的(在本例中 [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]);
                        }
                    }
                }
            }

注意:old_nodes 是“上一个”,$collections 是“$_POST”

I've a $_POST that sends an array. And i've a prevoious array with contains a key that could contain or not one of the values from teh $_POST.

For Example:

$_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 ) ) 

So i need to check if the $_POST itms already exists on the previuos array ([collection_id] key) and extract the new ones (in this case [1] => 10 [2] => 52) to ve added to the database and also get those which has changed that need to be removed from the database (replaced) by the new values.

This my current code but not working well...

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

NOTE: old_nodes is "Previous" and $collections is "$_POST"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再见回来 2024-10-16 17:38:12

尝试这样的事情:

$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);

Try something like this:

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文