[英]PHP - Check if values in one array exist in another array multi-dimensional array
I have the following two arrays. 我有以下两个数组。
This is a flat array (string: $second_names): 这是一个平面数组(字符串:$ second_names):
Array ( [0] => Cars [3] => Bikes [8] => Trucks ) //$second_names
I have this multidimensional array - (string: $premiumCatArraySets): 我有这个多维数组-(string:$ premiumCatArraySets):
Array
(
[0] => Array
(
[primary-category] => Automobiles
[secondary-category] => Cars
[tertiary-category] => Fiat Punto
)
[1] => Array
(
[primary-category] => Automobiles
[secondary-category] => Cars
[tertiary-category] => BMW
)
[2] => Array
(
[primary-category] => Automobiles
[secondary-category] => Bikes
[tertiary-category] => Honda
)
[4] => Array
(
[primary-category] => Automobiles
[secondary-category] => Trucks
[tertiary-category] => Iveco
)
[6] => Array
(
[primary-category] => Automobiles
[secondary-category] => Cars
[tertiary-category] => Mercedes
)
[9] => Array
(
[primary-category] => Automobiles
[secondary-category] => Cars
[tertiary-category] => Toyota
)
I am trying to use in_array
to see whether the values in the flat array exist and output the brand of the car. 我正在尝试使用
in_array
来查看平面数组中的值是否存在并输出汽车的品牌。
This is what I tried 这是我尝试过的
foreach ($second_names as $second_name) {//Vechile type e.g. car, truck, bike
if(in_array($second_name, $premiumCatArraySets)){
echo '<h2>'.$second_name.'</h2>';
foreach ($third_names as $third_name) {// e.g. Fiat, BMW, Toyota
echo $third_name.'<br/>';
}
}
}
The line for if(in_array($second_name, $premiumCatArraySets)){
doesn't seem to be displaying anything. if(in_array($second_name, $premiumCatArraySets)){
似乎没有显示任何内容。
If my understanding is correct, you have to get the vehicle brand from the second array for each vehicles in the first array. 如果我的理解是正确的,那么您必须从第二个数组中获得第一个数组中每个车辆的车辆品牌。 You could do something like below.
您可以执行以下操作。 This is a basic script.
这是一个基本脚本。
<?php
$vehicles = ['Cars', 'Bikes', 'Trucks'];
$details = [
[
'primary-category' => 'Automobiles',
'secondary-category' => 'Cars',
'tertiary-category' => 'BMW'
],
[
'primary-category' => 'Automobiles',
'secondary-category' => 'Trucks',
'tertiary-category' => 'Benz'
]
];
foreach ($vehicles as $vehicle) {
foreach ($details as $detail) {
if ($vehicle == $detail['secondary-category']) {
echo $detail['tertiary-category'];
break;
}
}
}
?>
Try 尝试
$output = [];
foreach($premiumCatArraySets as $key => $value){
if(in_array($value["secondary-category"],$second_names)){
if(!isset($output[$value["secondary-category"]])){
$output[$value["secondary-category"]] = [];
}
$output[$value["secondary-category"]][] = $value["tertiary-category"];
}
}
foreach($output as $key => $value){
echo '<h2>'.$key."</h2>";
echo implode(", ",$value)."<br/>";
}
Output 输出量
Cars
Fiat Punto, BMW, Mercedes, Toyota
Bikes
Honda
Trucks
Iveco
The solution using call_user_func_array
, array_merge_recursive
, array_keys
, array_flip
, array_intersect_key
and implode
functions: 使用
call_user_func_array
, array_merge_recursive
, array_keys
, array_flip
, array_intersect_key
和implode
函数的解决方案:
// grouping each category preserving the position of each item
$groups = call_user_func_array('array_merge_recursive', $premiumCatArraySets);
foreach ($second_names as $name) {
$indexes = array_flip(array_keys($groups['secondary-category'], $name));
echo '<h2>'.$name.'</h2>';
echo implode(", ", array_intersect_key($groups['tertiary-category'], $indexes)) .'<br/>';
}
The output: 输出:
<h2>Cars</h2>Fiat Punto, BMW, Mercedes, Toyota<br/><h2>Bikes</h2>Honda<br/><h2>Trucks</h2>Iveco<br/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.