filter multidemsional array based on speed in php

Viewed 44

I am facing one problem while filter the data from arrays. My array is look like below :-

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
    )
)

I want to filter and the result in multiple Arrays like when speed (>0) and if in between speed is 0 add in last position and breaks and again start with array to find to where speed (>0) and so on. My excepted output is like below :

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
            )

    )
 )

I have started the code below

 foreach($locations as $lkey=> $location){
        if($location['speed'] >0){
            
        }
    }

Can anyone hlep me how can i resolve this problem thanks in adavnce.

1 Answers

You can collect all intermediate arrays that are > 0 and if you find a zero, add zero to the intermediate result, push it to overall final result and empty the intermediate temp array, like below:

<?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);
Related