我有以下两个数组:
$a = [ 'post_type' => 'ese' ];
$b = [ 'demo_handle' => 'demo-3', 'post_type' => [ 'aaa', 'bbb' ], 'id' => 3'];我希望$b“采用”$a的值,但不是相反。
$c = [ ..., 'post_type' => [ 'aaa, 'bbb', 'ese'], ...];我怎样才能做到这一点?我尝试过多种方法,但似乎都没有正确的array_merge。
发布于 2018-10-08 17:42:32
使用循环与isset()函数。下面是代码注释中的更多细节:
// Loop over the $b array
foreach ($b as $key => $value) {
// Check if this key exists in $a array also
if (isset($a[$key])) {
// if exists, we can merge them
// We need to typecast value in $a to array
// since, array_merge requires array arguments
$b[$key] = array_merge((array)$value,
(array)$a[$key]);
}
}发布于 2018-10-08 17:45:07
PHP有一个名为array_merge_recursive的函数
$c = array_merge_recursive($a, $b);https://stackoverflow.com/questions/52707479
复制相似问题