[英]Combining 2 Arrays when value is matched
So I have these arrays that is fetched from 2 different database, I would like to combine them in one array when the ['item_name'] and ['name'] is matched then getting the ['id'] from the Array2 所以我有这些数组是从2个不同的数据库中提取的,当['item_name']和['name']匹配时,我想将它们合并到一个数组中,然后从Array2中获取['id']
I tried doing the in_array but since it's multi dimensional, I can't get the right output I want, I tried foreach also but I can't also get the right output or maybe I'm doing it wrong, I'm running out of idea how I could do the output I wanted. 我尝试做in_array,但是由于它是多维的,所以我无法获得所需的正确输出,我也尝试了foreach但也无法获得正确的输出,或者我做错了,我快要耗尽了想法我该怎么做我想要的输出。
Example Array1: Array1示例:
Array
(
[0] => Array
(
[item_id] => 1
[item_name] => Bag
[Color] => Purple
)
[1] => Array
(
[item_id] => 2
[item_name] => Pencil
[Color] => Yellow
)
[2] => Array
(
[item_id] => 3
[item_name] => Tumbler
[Color] => Blue
)
[3] => Array
(
[item_id] => 4
[item_name] => Shirt
[Color] => Red
)
)
Example Array2: Array2示例:
Array
(
[0] => Array
(
[id] => 11
[name] => Bag
)
[1] => Array
(
[id] => 22
[name] => Pencil
)
[2] => Array
(
[id] => 33
[name] => Tumbler
)
[3] => Array
(
[id] => 44
[name] => Shirt
)
[4] => Array
(
[id] => 55
[name] => Paper
)
[5] => Array
(
[id] => 66
[name] => Chair
)
[6] => Array
(
[id] => 4
[name] => Notebook
)
)
So my expected output would be: 所以我的预期输出将是:
Array
(
[0] => Array
(
[id] => 11
[name] => Bag
[Color] => Purple
)
[1] => Array
(
[id] => 22
[name] => Pencil
[Color] => Yellow
)
[2] => Array
(
[id] => 33
[name] => Tumbler
[Color] => Blue
)
[3] => Array
(
[id] => 44
[name] => Shirt
[Color] => Red
)
)
Try by using foreach loop. 通过使用foreach循环尝试。 Check if in foreach loop if any index value match with another array then create a new array with all those values. 检查是否在foreach循环中是否有任何索引值与另一个数组匹配,然后使用所有这些值创建一个新数组。 You can use counter here to get the value of array inside loop. 您可以在此处使用counter来获取循环内数组的值。
You can create a map with the names and the ID from the second array and then loop to modify the first one. 您可以使用第二个数组中的名称和ID创建一个映射,然后循环修改第一个数组。
Consider the following code: 考虑以下代码:
$a1 = array("item_id" => 1, "item_name" => "Bag", "Color" => "Purple");
$a2 = array("item_id" => 2, "item_name" => "Pencil", "Color" => "Yellow");
$a3 = array("item_id" => 3, "item_name" => "Tumbler", "Color" => "Blue");
$b1 = array("item_id" => 11, "item_name" => "Bag");
$b2 = array("item_id" => 22, "item_name" => "Pencil");
$b3 = array("item_id" => 33, "item_name" => "Tumbler");
$arr1 = array($a1, $a2, $a3);
$arr2 = array($b1, $b2, $b3);
$map = []; // here keys are names and value are the id
foreach($arr2 as $elem)
$map[$elem["item_name"]] = $elem["item_id"];
$ans = [];
foreach ($arr1 as $elem) {
if (array_key_exists($elem["item_name"], $map)) {
$elem["item_id"] = $map[$elem["item_name"]];
$ans[] = $elem;
}
}
echo print_r($ans);
In this way you achieve complexity of O(n)
instead of O(n^2)
if trying nested for-loop 这样,如果尝试嵌套的for循环,就可以实现O(n)
而不是O(n^2)
复杂性
Been trying to simplify my code earlier and I think I got it, thanks to anju and David Winder, if anyone interested how I did it, this is how: 一直在尝试简化我的代码,由于anju和David Winder的帮助,我想我明白了,如果有人对我的代码感兴趣,可以这样:
index = 0;
foreach ($array1 as $val){
foreach ($array2 as $val2){
if ($val['item_id'] == $val2['id']){
$filtered[$index]['id'] = $val2['id'];
$filtered[$index]['name'] = $val2['name'];
$filtered[$index]['color'] = $val['color'];
$index++;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.