phparraysmultidimensional-arrayarray-filterarray-walk

Is there any PHP array_* function for extracting single values from a multidimensional array?


If I have a multidimensional array, and I want to extract some of the data from it and place them in a new array, is there any existing array_*() function to do so?

For example, if I have the following array:

array(
    [
        'id'    => 1,
        'num'   => 200,
        'text'  => 'abc'
    ],
    [
        'id'    => 2,
        'num'   => 230,
        'text'  => 'def'
    ],
    [
        'id'    => 3,
        'num'   => 100,
        'text'  => 'ghi'
    ],

)

I would like to get the following resulting array:

[ 'abc', 'def', 'ghi' ]

Of course I can always do it manually using foreach() or something similar, but one-liners are always nice :)


Solution

  • Try array_column function;

    array_column(array $data, 'key')