In php is there a function like array_column for multidimensional arrays

get value from multidimensional array php
php array_column
php get value from multidimensional array by key
php multidimensional array
php search multidimensional array for multiple values
php get array value by key
php get all values from multidimensional array
php add to multidimensional array

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

)

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
                )

        )

)

In php is there a function like array_column for multidimensional arrays, A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of The column to use as the index/keys for the returned array. This value may if array_column does not exist the below solution will work. The array_column Function. PHP's array_column function is designed specifically for multidimensional arrays. Pass array_column a multidimensional array and a key (numeric or string), and it will return an array consisting of those values from each sub-array. We use a multidimensional array of products to demonstrate.


Oh I just noticed that you're aggregating them by ID. There's not a function for that, you're going to need to iterate over the input with a loop, and populate an output array with the data you want. Eg:

$output = [];

foreach($input_array as $item) {
  $output[$item['id']][] = [
     'itemID' => $item['itemID'],
     'itemName' => $item['itemName']
  ];
}

array_column - Manual, Here array_column() function will return the Values with 'Address' key from the $​users array, which is a multidimensional array. Output will be like  PHP - Multidimensional Arrays A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep.


Quite old question, but I hope it help someone. Unfortunately there's no native function yet but output is achievable using php's array_filter():

$foods = [
    [
        'foodType' => 'fruits',
        'itemID' => 1,
        'itemName' => 'apple',
    ],

    [
        'foodType' => 'fruits',
        'itemID' => 2,
        'itemName' => 'banana',
    ],

    [
        'foodType' => 'veggies',
        'itemID' => 3,
        'itemName' => 'carrot',
    ],

    [
        'foodType' => 'veggies',
        'itemID' => 4,
        'itemName' => 'broccoli',
    ]

];

$grouped_foods = [];
$groupByColumn = 'foodType';

array_filter($foods, function ($foodItem) use(&$grouped_foods, $groupByColumn) {
    $grouped_foods[$foodItem[$groupByColumn]][] = array_filter($foodItem, function ($key) use($groupByColumn) {
        return $key != $groupByColumn;
    }, ARRAY_FILTER_USE_KEY);
});


echo "<pre>";
print_R($grouped_foods);
echo "</pre>";

see in action: https://3v4l.org/bbX5A

Disclaimer: for/foreach loops are significantly faster in performance than native array functions.

How To Get Values From Multidimensional Arrays in PHP, () function is used to return the values from a single column in the input array. Using Multidimensional Arrays in PHP. Array elements in PHP can hold values of any type, such as numbers, strings and objects. They can also hold other arrays, which means you can create multidimensional, or nested, arrays.


I prefer using the following solution.

Example

  "allergens" => array:5 [
    0 => array:2 [
      "id" => "10"
      "object" => array:1 [
        "allergens" => "10"
      ]
    ]
    1 => array:2 [
      "id" => "11"
      "object" => array:1 [
        "allergens" => "11"
      ]
    ]
    2 => array:2 [
      "id" => "4"
      "object" => array:1 [
        "allergens" => "4"
      ]
    ]
  ]

Giving this example, if you would like an array containing only the value of allergens then use the following code.

Solution

$allergens = array_map( function ( $ar ) {
   return $ar['allergens'];
}, array_column( $allergensArr, 'object' ) );

Result

array:5 [
  0 => "10"
  1 => "11"
  2 => "4"
]

PHP Array Column Example, Sorts an associative array in descending order, according to the key Question: Tag: php,arrays,sorting,multidimensional-array Sorting a multidimensional array by value is fairly easy and has been answered multiple times already here


Multidimensional Arrays in PHP | PHP Tutorial, Sorts an associative array in ascending order, according to the key array_column. Beschreibung. array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.


PHP Array Functions, Specifies the multi-dimensional array (record-set) to use. As of PHP 7.0, this can also be an array of objects. column_key, Required. An integer key or a string  While PHP has well over three-score array functions, array_rotate is strangely missing as of PHP 5.3. Searching online offered several solutions, but the ones I found have defects such as inefficiently looping through the array or ignoring keys.


PHP array_column() Function, Maybe with array functions or something. array_filter() and array_column() come to mind. 1 Like. Array_column For Multidimensional Array Article Browse our Array_column For Multidimensional Array reference - you might also be interested in Jägermeister Alko (in 2020) and Netgarn . by Dax Lagnese