In php is there a function like array_column for multidimensional arrays

jcropp picture jcropp · May 5, 2018 · Viewed 7.8k times · Source

Is there function that works similar to array_column for multidimensional arrays? Is there a function that translates the first array below to the second:

Array
(
    [0] => Array
        (
            [foodType] => fruits
            [itemID] => 1
            [itemName] => apple
        )

    [1] => Array
        (
            [foodType] => fruits
            [itemID] => 2
            [itemName] => banana
        )

    [2] => Array
        (
            [foodType] => veggies
            [itemID] => 3
            [itemName] => carrot
        )

    [3] => Array
        (
            [foodType] => veggies
            [itemID] => 4
            [itemName] => broccoli
         )

)

Resulting array:

Array
(
    [fruits] => Array
        (
            [0] => Array
                (
                    [itemID] => 1
                    [itemName] => apple
                )

            [1] => Array
                (
                    [itemID] => 2
                    [itemName] => banana
                )

        )

    [veggies] => Array
        (
            [0] => Array
                (
                    [itemID] => 3
                    [itemName] => carrot
                )

            [1] => Array
                (
                    [itemID] => 4
                    [itemName] => broccoli
                 )
        )

)

Answer

Lawrence Cherone picture Lawrence Cherone · May 5, 2018

No, there is not a function to get your expected output natively, though you can make your own functions, just use array_column to get the types/column, and then loop over your array, on match remove the item as to not duplicate iterations.

Something like:

<?php
$data = [
    ['foodType' => 'fruits', 'itemID' => 1, 'itemName' => 'apple'],
    ['foodType' => 'fruits', 'itemID' => 2, 'itemName' => 'banana'],
    ['foodType' => 'veggies', 'itemID' => 3, 'itemName' => 'carrot'],
    ['foodType' => 'veggies', 'itemID' => 4, 'itemName' => 'broccoli']
];

function array_column_multi ($array, $column) {
    $types = array_unique(array_column($array, $column));

    $return = [];
    foreach ($types as $type) {
        foreach ($array as $key => $value) {
            if ($type === $value[$column]) {
                unset($value[$column]);
                $return[$type][] = $value;
                unset($array[$key]);
            }
        }
    }
    return $return;
}


print_r(array_column_multi($data, 'foodType'));

https://3v4l.org/KQVeN

Result:

Array
(
    [fruits] => Array
        (
            [0] => Array
                (
                    [itemID] => 1
                    [itemName] => apple
                )

            [1] => Array
                (
                    [itemID] => 2
                    [itemName] => banana
                )

        )

    [veggies] => Array
        (
            [0] => Array
                (
                    [itemID] => 3
                    [itemName] => carrot
                )

            [1] => Array
                (
                    [itemID] => 4
                    [itemName] => broccoli
                )

        )

)