phparraysmultidimensional-arraygrouping

Split a 2d array into two 2d grouping arrays by column value


Is there a PHP function, or other solution, that will facilitate splitting an array based on a value from it's subarrays?

Yes, I know I can do this with a loop! The question is if there's another way to do it without having loop through.

Example:

Using the value of Active, turn this array...

$array_all => Array
(
    [126] => Array
        (
            [DisplayName] => Customer ABC
            [Active] => 1
        )

    [1596] => Array
        (
            [DisplayName] => Customer 123
            [Active] => 0
        )

    [1648] => Array
        (
            [DisplayName] => John Q Sample
            [Active] => 1
        )

    [1649] => Array
        (
            [DisplayName] => Fry & Leela, Inc.
            [Active] => 0
        )

    [1571] => Array
        (
            [DisplayName] => Class Action: Redshirts vs. UFP 
            [Active] => 1
        )
)

...into this array...

$array_active => Array
(
    [126] => Array
        (
            [DisplayName] => Customer ABC
            [Active] => 1
        )

    [1648] => Array
        (
            [DisplayName] => John Q Sample
            [Active] => 1
        )

    [1571] => Array
        (
            [DisplayName] => Class Action: Redshirts vs. UFP 
            [Active] => 1
        )
)

... and this array.

$array_inactive => Array
(

    [1596] => Array
        (
            [DisplayName] => Customer 123
            [Active] => 0
        )

    [1649] => Array
        (
            [DisplayName] => Fry & Leela, Inc.
            [Active] => 0
        )

)

Solution

  • You could use array_filter:

    $actives = array_filter($array_all, function ($row) {
        return $row["Active"];
    }); 
    
    $notActives = array_filter($array_all, function ($row) {
        return !$row["Active"];
    }); 
    

    You can also use array_reduce as alternative, but it returns indexed arrays, so without the original keys:

    list($actives, $notActives) = array_reduce($array_all, function ($result, $row) {
        $result[$row["Active"]][] = $row;
        return $result;
    }, [[],[]]);
    

    When using array_reduce to also maintain keys, it becomes quite verbose:

    list($actives, $notActives) = array_reduce(array_keys($array_all), 
        function ($result, $key) use ($array_all) {
            $result[$array_all[$key]["Active"]][$key] = $array_all[$key];
            return $result;
        }, [[],[]]
    );