I seem to get stuck at a (at first glance) simple thing: I have a nested assoc array ie
"stock" => [
123 => [
3 => 17,
5 => 5,
7 => 0
],
456 => [
3 => 1,
5 => 3,
7 => 7
]
]
These represent stocks of items (123 and 456) in warehouses (3, 5 and 7). Now I want to update stock in one warehouse for one item ie
"stock" => [
123 => [
3 => 11
]
]
I have tried to approach this via array_merge (re-keys the array) and array_merge_recursive (strangely does the same while it shouldn't - are numeric keys the reason?) but to no avail. Also I found this interesting bit https://vancelucas.com/blog/php-array_merge-preserving-numeric-keys/ but that replaces the entire branch of the array (so I'm losing warehouses 5 and 7).
Does anyone have some clever idea about this?
This should be enough:
$newArray = array_replace_recursive($sourceArray, $newDataArray);
Fiddle here https://3v4l.org/qdejB
Here is your snippet,
foreach ($temp['stock'] as $key => &$value) {
foreach ($value as $key1 => $value1) {
// no need to check, it will add at respective key matching
$arr['stock'][$key][$key1] = $value1;
}
}
如果我错了,请纠正我,但似乎没有什么能阻止您:
$foo['stock'][123][3] = 11;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.