英文:
How can I find an replace an array value based on another value in the same multidimensional array
问题
所以我有一个多维数组,像下面这样:
[0]=>
array(4) {
["id"] => 110
["list"] => {}
["MainProduct"] => 2 <- 步骤2:当productName是Oranges时,将2替换为5
["productName"] => Oranges <- 步骤1:找到包含"Oranges"的位置
}
[1]=>
array(4) {
["id"] => 111
["list"] => {}
["MainProduct"] => 110
["productName"] => Apples
}
最终,我需要实现的是找到`key`为`productName`且`value`为`Oranges`的位置,然后基于此替换同一子数组中`key`为`MainProduct`的`value`。
我想我应该先找到包含`Oranges`的数组的索引,然后替换同一子数组中`key`为`mainProduct`的`value`,但似乎无法正确执行。
`key`名称始终相同,而`value`名称不同。
另外,数组是动态的,所以包含`Oranges`的数组的索引不会总是相同的。
这是期望的结果:
[0]=>
array(4) {
["id"] => 110
["list"] => {}
["MainProduct"] => 5 <- 当productName是"Oranges"时,将其更改为5
["productName"] => Oranges
}
[1]=>
array(4) {
["id"] => 111
["list"] => {}
["MainProduct"] => 110
["productName"] => Apples
}
英文:
So I have a multidimensional array like the one below:
[0]=>
array(4) {
["id"] => 110
["list"] => {}
["MainProduct"] => 2 <- Step2: Replace the 2 with a 5 where productName is Oranges
["productName"] => Oranges <- Step1: Find where we have "Oranges"
}
[1]=>
array(4) {
["id"] => 111
["list"] => {}
["MainProduct"] => 110
["productName"] => Apples
}
Ultimately, what I need to achieve is to find were the value
for key
productName equals Oranges
and based on that to replace the value
for key
MainProduct in the same sub array.
I thought I should first find the index for the array containing Oranges
and then replace the value
for key
mainProduct but can't seem to do it properly.
They key
names are always the same, while the value
names are different.
Also, the array is dynamic so the array with Oranges
won't always have the same index.
This is the desired result:
[0]=>
array(4) {
["id"] => 110
["list"] => {}
["MainProduct"] => 5 <- This is changed to 5 where productName is "Oranges"
["productName"] => Oranges
}
[1]=>
array(4) {
["id"] => 111
["list"] => {}
["MainProduct"] => 110
["productName"] => Apples
}
答案1
得分: 1
一个稍微变化的策略,你提出的:
<?php
$data = [
[
"id" => 110,
"MainProduct" => 2,
"productName" => "橙子",
],
[
"id" => 111,
"MainProduct" => 110,
"productName" => "苹果",
]
];
$output = [];
array_walk($data, function(&$entry) use($data) {
$mainProduct = array_search($entry['MainProduct'], array_column($data, 'id'));
$entry['productName'] = $data[$mainProduct]['productName'];
});
print_r($data);
英文:
A slight variant of the strategy you proposed:
<?php
$data = [
[
"id" => 110,
"MainProduct" => 2,
"productName" => "Oranges",
],
[
"id" => 111,
"MainProduct" => 110,
"productName" => "Apples",
]
];
$output = [];
array_walk($data, function(&$entry) use($data) {
$mainProduct = array_search($entry['MainProduct'], $data);
$entry['productName'] = $data[$mainProduct]['productName'];
});
print_r($data);
The output is:
Array
(
[0] => Array
(
[id] => 110
[MainProduct] => 2
[productName] => Oranges
)
[1] => Array
(
[id] => 111
[MainProduct] => 110
[productName] => Oranges
)
)
答案2
得分: 0
如果你获取了键并与productName
的值结合,然后你可以搜索并获取到这个键。然后使用这个键来更新MainProduct
:
$array[array_search('Oranges', array_combine(array_keys($array), array_column($array, 'productName')))]['MainProduct'] = 5;
如果productName
是唯一的,那么我会直接修改数组,以便以此为索引:
$array = array_column($array, null, 'productName');
$array['Orange']['MainProduct'] = 5;
英文:
If you get the keys and combine with the productName
values, then you can search it and get the key. Then use the key to update MainProduct
:
$array[array_search('Oranges', array_combine(array_keys($array), array_column($array, 'productName')))]['MainProduct'] = 5;
If productName
is unique, then I would just modify the array to index on that:
$array = array_column($array, null, 'productName');
$array['Orange']['MainProduct'] = 5;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论