合并具有公共id的多维数组
Merge multi-dimensional arrays with common id
我有两个多维数组:
第一批披萨:
0 => array:2 [▼
"pizza_id" => 11
"pizza_name" => "Hawaï"
]
1 => array:2 [▼
"pizza_id" => 12
"pizza_name" => "Tonno"
]
2 => array:2 [▼
"pizza_id" => 13
"pizza_name" => "Surprise"
]
第二列成分:
0 => array:4 [▼
"pizza_id" => 11
"ingredient_one" => "Ananas"
"ingredient_two" => "Onion"
"ingredient_three" => "Ham"
]
1 => array:4 [▼
"pizza_id" => 12
"ingredient_one" => "Tuna"
"ingredient_two" => "Onion"
"ingredient_three" => null
]
我想合并这两个通过 pizza_id
链接的数组。其中一个 Pizza ID 没有匹配的配料数组(配料未知)所以对于这个 Pizza,我想将配料设置为 null
并获取下一个数组:
新数组 PizzaAndIngredients:
0 => array:5 [▼
"pizza_cid" => 11
"pizza_name" => "Hawaï"
"ingredient_one" => "Ananas"
"ingredient_two" => "Onion"
"ingredient_three" => "Ham"
]
1 => array:5 [▼
"pizza_id" => 12
"pizza_name" => "Tonno"
"ingredient_one" => "Tuna"
"ingredient_two" => "Onion"
"ingredient_three" => null
]
2 => array:5 [▼
"pizza_id" => 13
"pizza_name" => "Surprise"
"ingredient_one" => null
"ingredient_two" => null
"ingredient_three" => null
]
我一直在摆弄array_merge
、foreach
等,但无法获取数组。另一种方法是构建一个新查询并左连接比萨饼和配料,但在现实生活中,数组要复杂得多,这意味着要重复大量代码。
我希望有一个更有效的合并两个多维数组的解决方案。
我最后一次尝试是:
$PizzaAndIngredients = array();
foreach ($pizzas as $key => $values) {
$PizzaAndIngredients[$key] = array_merge($ingredients, $values);
}
当您从查询结果中获取行时,您可以将数组键设置为等于 pizza_id
,或者通过 pizza_id
:
重新索引它们
$pizza = array_column($pizza, null, 'pizza_id');
$ingredients = array_column($ingredients, null, 'pizza_id');
然后创建一个默认的 null
数组以供在 $ingredients
不存在时使用,然后将 $ingredients
值与 $pizza
值合并:
//get an ingredient array
$temp = reset($ingredients);
//combine keys and an array of nulls
$default = array_combine(array_keys($temp),
array_fill(0, count($temp), null));
foreach($pizza as $id => $p_values) {
//check if $ingredients[$id] exists, if not use the default
$i_values = isset($ingredients[$id]) ? $ingredients[$id] : $default;
//you can also use just $result[] if you don't need the pizza_id as key
$result[$id] = array_merge($i_values, $p_values);
}
我有两个多维数组:
第一批披萨:
0 => array:2 [▼
"pizza_id" => 11
"pizza_name" => "Hawaï"
]
1 => array:2 [▼
"pizza_id" => 12
"pizza_name" => "Tonno"
]
2 => array:2 [▼
"pizza_id" => 13
"pizza_name" => "Surprise"
]
第二列成分:
0 => array:4 [▼
"pizza_id" => 11
"ingredient_one" => "Ananas"
"ingredient_two" => "Onion"
"ingredient_three" => "Ham"
]
1 => array:4 [▼
"pizza_id" => 12
"ingredient_one" => "Tuna"
"ingredient_two" => "Onion"
"ingredient_three" => null
]
我想合并这两个通过 pizza_id
链接的数组。其中一个 Pizza ID 没有匹配的配料数组(配料未知)所以对于这个 Pizza,我想将配料设置为 null
并获取下一个数组:
新数组 PizzaAndIngredients:
0 => array:5 [▼
"pizza_cid" => 11
"pizza_name" => "Hawaï"
"ingredient_one" => "Ananas"
"ingredient_two" => "Onion"
"ingredient_three" => "Ham"
]
1 => array:5 [▼
"pizza_id" => 12
"pizza_name" => "Tonno"
"ingredient_one" => "Tuna"
"ingredient_two" => "Onion"
"ingredient_three" => null
]
2 => array:5 [▼
"pizza_id" => 13
"pizza_name" => "Surprise"
"ingredient_one" => null
"ingredient_two" => null
"ingredient_three" => null
]
我一直在摆弄array_merge
、foreach
等,但无法获取数组。另一种方法是构建一个新查询并左连接比萨饼和配料,但在现实生活中,数组要复杂得多,这意味着要重复大量代码。
我希望有一个更有效的合并两个多维数组的解决方案。
我最后一次尝试是:
$PizzaAndIngredients = array();
foreach ($pizzas as $key => $values) {
$PizzaAndIngredients[$key] = array_merge($ingredients, $values);
}
当您从查询结果中获取行时,您可以将数组键设置为等于 pizza_id
,或者通过 pizza_id
:
$pizza = array_column($pizza, null, 'pizza_id');
$ingredients = array_column($ingredients, null, 'pizza_id');
然后创建一个默认的 null
数组以供在 $ingredients
不存在时使用,然后将 $ingredients
值与 $pizza
值合并:
//get an ingredient array
$temp = reset($ingredients);
//combine keys and an array of nulls
$default = array_combine(array_keys($temp),
array_fill(0, count($temp), null));
foreach($pizza as $id => $p_values) {
//check if $ingredients[$id] exists, if not use the default
$i_values = isset($ingredients[$id]) ? $ingredients[$id] : $default;
//you can also use just $result[] if you don't need the pizza_id as key
$result[$id] = array_merge($i_values, $p_values);
}