Search code examples
phparraysmultidimensional-arrayfilteringnumeric

Filter a 2d array and retain rows where a column value falls within a qualifying numeric range


I can't figure out how to get the desired values. I am trying to filter my array on it's values.

#current array
Array
(
    [0] => Array
        (
            [0] => Product1 
            [1] => Description product 1
            [2] => 10
        )

    [1] => Array
        (
            [0] => Product2
            [1] => Description product 2
            [2] => 20       
        )

    [2] => Array
        (
            [0] => Product3
            [1] => Description product 3  
            [2] => 30    
        )

    [3] => Array
        (
            [0] => Product4
            [1] => Description product 4  
            [2] => 40    
        )

    [4] => Array
        (
            [0] => Product5
            [1] => Description product 5  
            [2] => 50    
        )
)

#resultant array
Array
(
    [0] => Array
        (
            [0] => Product3 
            [1] => Description product 3
            [2] => 30
        )

    [1] => Array
        (
            [0] => Product4
            [1] => Description product 4
            [2] => 40       
        )
)

As you can see in my block of code, I am trying to create a new array filtered by >= and <=. For example the #resultant array only contains records where [2] is bigger (>=) than 30 and lower or equal (<=) than 40.

I did find an answer for a non-dimensional array, but I can't figure out how to use it for my application see: php numeric array select values greater than a number and lower than another and save it to a new array

I just can't figure out how to write/build this code, I also would like two variables; $min = 30 and $max = 40 for example.


Solution

  • You can use array_filter to filter your array based on conditions:

    <?php
        $data = array
        (
            0 => array
            (
                0 => 'Product1',
                1 => 'Description product 1',
                2 => '10'
            ),
            1 => array
            (
                0 => 'Product2',
                1 => 'Description product 2',
                2 => '20'
            ),
            2 => array
            (
                0 => 'Product3',
                1 => 'Description product 3',
                2 => '30'
            ),
            3 => array
            (
                0 => 'Product4',
                1 => 'Description product 4',
                2 => '40'
            ),
            4 => array
            (
                0 => 'Product5',
                1 => 'Description product 5',
                2 => '50'
            )
        );
    
        $data = array_filter($data, function($el)
        {
            return ($el[2] >= 30 && $el[2] <= 40);
        });
    
        echo '<pre>'. print_r($data, 1) .'</pre>';
    

    Here you can just pass a function into the second callback and set your conditions that way.