php中基于速度的过滤多维数组
在从数组中筛选数据时,我面临一个问题。我的数组如下所示:-php中基于速度的过滤多维数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,在从数组中筛选数据时,我面临一个问题。我的数组如下所示:- Array ( [0] => Array ( [id] => 109729 [address] => Panipat, Haryana, IN [speed] => 0 ) [1] => Array ( [id] => 109730 [address] => Panipat, Haryana
Array
(
[0] => Array
(
[id] => 109729
[address] => Panipat, Haryana, IN
[speed] => 0
)
[1] => Array
(
[id] => 109730
[address] => Panipat, Haryana, IN
[speed] => 5.55
)
[2] => Array
(
[id] => 109731
[address] => Panipat, Haryana, IN
[speed] => 6.555
)
[3] => Array
(
[id] => 109732
[address] => Panipat, Haryana, IN
[speed] => 0
)
[4] => Array
(
[id] => 109733
[address] => Panipat, Haryana, IN
[speed] => 0
)
[5] => Array
(
[id] => 109734
[address] => Panipat, Haryana, IN
[speed] => 2.5
)
[6] => Array
(
[id] => 109735
[address] => Panipat, Haryana, IN
[speed] => 4.5
)
[7] => Array
(
[id] => 109736
[address] => Panipat, Haryana, IN
[speed] => 10
)
[8] => Array
(
[id] => 109737
[address] => Panipat, Haryana, IN
[speed] => 0
)
)
我想在多个数组中过滤结果,比如当速度(>0)时,如果介于速度为0时,则添加最后一个位置和断点,然后再次从数组开始查找速度(>0)的位置,等等。我的预期输出如下:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 109730
[address] => Panipat, Haryana, IN
[speed] => 5.55
)
[1] => Array
(
[id] => 109731
[address] => Panipat, Haryana, IN
[speed] => 6.555
)
[2] => Array
(
[id] => 109732
[address] => Panipat, Haryana, IN
[speed] => 0
)
)
[1] => Array
(
[0] => Array
(
[id] => 109734
[address] => Panipat, Haryana, IN
[speed] => 2.5
)
[1] => Array
(
[id] => 109735
[address] => Panipat, Haryana, IN
[speed] => 4.5
)
[2] => Array
(
[id] => 109736
[address] => Panipat, Haryana, IN
[speed] => 10
)
[3] => Array
(
[id] => 109737
[address] => Panipat, Haryana, IN
[speed] => 0
)
)
)
我已经开始了下面的代码
foreach($locations as $lkey=> $location){
if($location['speed'] >0){
}
}
任何人都可以帮助我如何解决此问题,这要感谢adavnce。您可以收集所有大于0的中间数组,如果找到零,则将零添加到中间结果,将其推送到总体最终结果并清空中间临时数组,如下所示:
<?php
$result = [];
$temp = [];
foreach($locations as $lkey => $location){
$temp[] = $location;
if($location['speed'] == 0){
if(count($temp) > 1){ // meaning this batch has at least 1 entry with speed > 0
$result[] = $temp;
}
$temp = [];
}
}
if(count($temp) > 0){
$result[] = $temp;
$temp = [];
}
print_r($result);
第一个元素发生了什么?为什么不包括它,而包括其他0速度元素?我们不能包括第一个元素,因为速度为零。我需要找到中间的停顿,所以我们需要每个数组的0个最后元素。我明白了,只捕获速度的降低,而不开始0。@El_Vanja你能帮我解决这个问题吗?