将关联数组中的值包含到多维关联数组中
Include value from associative array into multidimensional associative array
我想在 $arr1
的基础上使用 $arr2
的 special
行。
$arr1 = Array
(
[0] => Array
(
[1111] => Array
(
[itemid] => 4321
[itemcode] => item1
[avail] => 0
)
[2222] => Array
(
[itemid] => 4321
[itemcode] => item2
[avail] => 20
)
)
[1] => Array
(
[3333] => Array
(
[itemid] => 1342
[itemcode] => item3
[avail] => 10
)
)
)
$arr2 = Array
(
[1111] => Array
(
[itemid] => 4321
[itemcode] => item1
[special] => 10
)
[2222] => Array
(
[itemid] => 4321
[itemcode] => item2
[special] => 2
)
[3333] => Array
(
[itemid] => 1342
[itemcode] => item3
[special] => 1
)
)
我想要的结果是这样的:
$arr1 = Array
(
[0] => Array
(
[1111] => Array
(
[itemid] => 4321
[itemcode] => item1
[avail] => 0
[special] => 10
)
[2222] => Array
(
[itemid] => 4321
[itemcode] => item2
[avail] => 20
[special] => 2
)
)
[1] => Array
(
[3333] => Array
(
[itemid] => 1342
[itemcode] => item3
[avail] => 10
[special] => 1
)
)
)
我知道如何用两个多维数组来做到这一点,但是围绕 $arr1
的附加数组让我觉得我需要 for 循环,但我不想制作第三个新数组,只是接受$arr1
.
的特殊值
我试过以下变体:
$arr1 = array_map(function($v) use($arr2){
$v = (array)$v;
$key = array_search(array_column($v["itemcode"]), array_column($arr2, 'itemcode'));
$v["special"] = $arr2[$key]["special"];
return $v;
}, $arr1);
即使新索引出现在 $arr1
(new) 中,也无法将数据附加到 special
索引。
非常感谢指导!地图很酷也很强大,但我似乎无法完全领会它们的魅力。
只需使用嵌套的 foreach
循环。使用引用变量,以便您可以就地修改原始数组。
foreach ($arr1 as &$el1) {
foreach ($el1 as $key => &$el2) {
$el2['special'] = $arr2[$key]['special'];
}
}
不用引用修改$arr1
的另一种方法:
foreach($arr1 as $k => $v){
foreach($v as $idx => $elem) {
if(array_key_exists($idx, $arr2)) {
$arr1[$k][$idx]['special'] = $arr2[$idx]['special'];
}
}
}
我想在 $arr1
的基础上使用 $arr2
的 special
行。
$arr1 = Array
(
[0] => Array
(
[1111] => Array
(
[itemid] => 4321
[itemcode] => item1
[avail] => 0
)
[2222] => Array
(
[itemid] => 4321
[itemcode] => item2
[avail] => 20
)
)
[1] => Array
(
[3333] => Array
(
[itemid] => 1342
[itemcode] => item3
[avail] => 10
)
)
)
$arr2 = Array
(
[1111] => Array
(
[itemid] => 4321
[itemcode] => item1
[special] => 10
)
[2222] => Array
(
[itemid] => 4321
[itemcode] => item2
[special] => 2
)
[3333] => Array
(
[itemid] => 1342
[itemcode] => item3
[special] => 1
)
)
我想要的结果是这样的:
$arr1 = Array
(
[0] => Array
(
[1111] => Array
(
[itemid] => 4321
[itemcode] => item1
[avail] => 0
[special] => 10
)
[2222] => Array
(
[itemid] => 4321
[itemcode] => item2
[avail] => 20
[special] => 2
)
)
[1] => Array
(
[3333] => Array
(
[itemid] => 1342
[itemcode] => item3
[avail] => 10
[special] => 1
)
)
)
我知道如何用两个多维数组来做到这一点,但是围绕 $arr1
的附加数组让我觉得我需要 for 循环,但我不想制作第三个新数组,只是接受$arr1
.
我试过以下变体:
$arr1 = array_map(function($v) use($arr2){
$v = (array)$v;
$key = array_search(array_column($v["itemcode"]), array_column($arr2, 'itemcode'));
$v["special"] = $arr2[$key]["special"];
return $v;
}, $arr1);
即使新索引出现在 $arr1
(new) 中,也无法将数据附加到 special
索引。
非常感谢指导!地图很酷也很强大,但我似乎无法完全领会它们的魅力。
只需使用嵌套的 foreach
循环。使用引用变量,以便您可以就地修改原始数组。
foreach ($arr1 as &$el1) {
foreach ($el1 as $key => &$el2) {
$el2['special'] = $arr2[$key]['special'];
}
}
不用引用修改$arr1
的另一种方法:
foreach($arr1 as $k => $v){
foreach($v as $idx => $elem) {
if(array_key_exists($idx, $arr2)) {
$arr1[$k][$idx]['special'] = $arr2[$idx]['special'];
}
}
}