基本上我需要搜索一个数组并替换其数据
主要思想是从旧数组中查找数据并验证它们是否存在于新数组中(如果没有添加的话)
这是我的旧数组
$array1 = array(
array('id' => '1','data1' => 'ect bla bla'),
array('id' => '2','data2' => 'ect bla bla'),
array('id' => '3','data3' => 'ect bla bla'),
array('id' => '6','data3' => 'new data'),
array('id' => '100','data3' => 'this data is added if it does not exist in the array')
);
这是我的新数组
$array2 = array(
array('id' => '1','data1' => 'new data'), //example data that are modified
array('id' => '2','data2' => 'new data'), //example data that are modified
array('id' => '3','data3' => 'new data'), //example data that are modified
array('id' => '4','data3' => 'new data'),
array('id' => '5','data3' => 'new data'),
array('id' => '6','data3' => 'new data') //example data that are modified
);
这部分我不知道要放什么
foreach ($array1 as $value) {
//this is the id.. I imagine that here is where all the magic goes xd
$value['id']
}
我需要这个结果
print_r($array2);
Array
(
[0] => Array
(
[id] => 1
[data1] => ect bla bla
)
[1] => Array
(
[id] => 2
[data2] => ect bla bla
)
[2] => Array
(
[id] => 3
[data3] => ect bla bla
)
[3] => Array
(
[id] => 4
[data3] => new data
)
[4] => Array
(
[id] => 5
[data3] => new data
)
[5] => Array
(
[id] => 6
[data3] => ect bla bla
)
[6] => Array
(
[id] => 7
[data3] => this data is added if it does not exist in the array
)
)
答案 0 :(得分:1)
尝试array_replace_recursive($array2, $array1);
http://us3.php.net/manual/en/function.array-replace-recursive.php
这将提供以下输出:
Array
(
[0] => Array
(
[id] => 1
[data1] => ect bla bla
)
[1] => Array
(
[id] => 2
[data2] => ect bla bla
)
[2] => Array
(
[id] => 3
[data3] => ect bla bla
)
[3] => Array
(
[id] => 6
[data3] => new data
)
[4] => Array
(
[id] => 100
[data3] => this data is added if it does not exist in the array
)
[5] => Array
(
[id] => 6
[data3] => new data
)
)
然后使其独特使用
array_map("unserialize", array_unique(array_map("serialize", $array3)));