2 回答
TA贡献1848条经验 获得超6个赞
您可以将数组映射与多个数组一起使用,
$c = array_filter(array_map(function ($a1, $b1) {
if ($a1['param'] == $b1['param']) {
$a1['value'] = $b1['value'];
return $a1;
} else {
return $a1;
}
}, $a, $b));
编辑
$bData = array_column($b,'value','param'); // param as key and value as value
foreach($a as &$v){
$v['value'] = ($bData[$v['param']] ?? $v['value']); // php 7+
//or $v['value'] = (!empty($bData[$v['param']]) ? $bData[$v['param']] : $v['value']); // php 5.6 or previous
}
输出
Array
(
[0] => Array
(
[param] => banana
[value] => brown
)
[1] => Array
(
[param] => lemon
[value] => 4
)
[2] => Array
(
[param] => apple
[value] => 18
)
)
TA贡献1827条经验 获得超9个赞
$a = ['banana' => 'yellow', 'apple' => 3, 'lemon' => 18];
$b = ['banana' => 'brown', 'orange' => 3, 'lemon' => 4];
$a_keys = array_keys($a);
$c = $a;
foreach ($a_keys as $key) {
if(array_key_exists($key, $b)){
$c[$key] = $b[$key];
}
}
- 2 回答
- 0 关注
- 166 浏览
添加回答
举报