我发送了一个发送数组的$ _POST。而且我有一个包含一个键的prevoious数组,该键可以包含或不包含来自某个$ _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 ) )
所以我需要检查previuos数组([collection_id]键)上是否已经存在$ _POST itms,并将新的(在这种情况下为[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是“Previous”,$ collections是“$ _POST”
答案 0 :(得分:0)
尝试这样的事情:
$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);