[英]PHP associative multidimentional array - get values between array keys
I have a multidimentional, associative php array that looks like this:我有一个多维、关联的 php 数组,如下所示:
$transportation_options = Array
(
[0] => Array("label" => "Car", "group" => "start"),
[1]=> Array("label" => "Volvo", "group" => Null),
[2]=> Array("label" => "BMW", "group" => Null),
[3]=> Array("label" => "Saab", "group" => Null),
[4]=> Array("label" => "Land Roover", "group" => Null),
[5]=> Array("label" => "Car", "group" => "end"),
[6]=> Array("label" => "Air Plane", "group" => NULL)
[7]=> Array("label" => "Boat", "group" => "start"),
[8]=> Array("label" => "Ship", "group" => Null),
[9]=> Array("label" => "Yacht", "group" => Null),
[10]=> Array("label" => "Sail Boat", "group" => Null),
[11]=> Array("label" => "Boat", "group" => "end"),
[12]=> Array("label" => "Bicycle", "group" => NULL)
);
How can this array be turned into this array这个数组怎么变成这个数组
$transportation_options_rewrite = array
(
array("Car" => array(Volvo,BMW,Saab,Land Roover)),
array("Air Plane")
array("Boat" => array(Ship,Yacht,Sail Boat)),
array("Bicycle")
);
So that I (somehow) can compare the这样我(以某种方式)可以比较
$transportation_selected = array(Saab,Volvo,Ship,Bicycle,Yacht);
to the $transportation_options_rewrite and ultimately get the Following result:到 $transportation_options_rewrite 并最终得到以下结果:
Bicycle
Boat: Ship, Yacht
Car: Saab, Volvo
At this point I am stuck at the array rewrite, have been trying to follow this example.此时我被困在数组重写中,一直在尝试遵循这个例子。 But this does not seem work due to the multidimention array.
但是由于多维数组,这似乎不起作用。
$start = "start";
$end ="end";
$new_array = [];
$i=0;$go=false;
foreach ($transportation_options_rewrite as $element) {
if($go){
$new_array[$i] = $element;
$i++;
}
if($element==$start){
$go = true;
}
if($element==$end){
$go = false;
}
}
$total_elems_new = count($new_array);
unset($new_array[$total_elems_new-1]);
print_r($new_array);
The gist here is that we are either "in a group" or "NOT in a group".这里的要点是我们要么“在一个组中”,要么“不在一个组中”。 If we're not in a group, then a new value IS a group & we are then IN that group.
如果我们不在一个组中,那么一个新值就是一个组,然后我们就在那个组中。 If we ARE in a group, then the new value goes to the group we're in.
如果我们在一个组中,那么新值将进入我们所在的组。
If we're ending a group, then nothing is added to the output如果我们要结束一个组,则不会向 output 添加任何内容
This provides the output you described:这提供了您描述的 output:
$options = array
(
array("label" => "Car", "group" => "start"),
array("label" => "Volvo", "group" => Null),
array("label" => "BMW", "group" => Null),
array("label" => "Saab", "group" => Null),
array("label" => "Land Roover", "group" => Null),
array("label" => "Car", "group" => "end"),
array("label" => "Air Plane", "group" => NULL),
array("label" => "Boat", "group" => "start"),
array("label" => "Ship", "group" => Null),
array("label" => "Yacht", "group" => Null),
array("label" => "Sail Boat", "group" => Null),
array("label" => "Boat", "group" => "end"),
array("label" => "Bicycle", "group" => NULL)
);
$sorted = [];
$in = NULL;
foreach($options as $index=>$row){
// for version 2, uncomment the next line if ($in==null)$sorted...
// if ($in==null)$sorted[$row['label']] = [];
if ($row['group']=='start'){
$in = $row['label'];
continue;
}
if ($row['group']=='end'){
$in = null;
continue;
}
// For version 2, comment the next line if($in==null)
if ($in==null)$sorted[] = $row['label'];
if ($in!==null)$sorted[$in][] = $row['label'];
}
print_r($sorted);
I also put code to output in a slightly different format, where every group is an array-key & each group has an array (though Air Plain & Bicycle are empty arrays).我还以稍微不同的格式将代码放入 output,其中每个组都是一个数组键,每个组都有一个数组(尽管 Air Plain 和 Bicycle 是空数组)。 To make it do that, just comment/uncomment the lines noted in the code
要做到这一点,只需注释/取消注释代码中记录的行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.