phparraysmultidimensional-array

Create simple array from column of multidimensional array


If I have a multidimensional array containing data from certain mysql table, such as:

Array (
    [0] => Array (
        [0] => 5255071
        [id] => 5255071
        [1] => 2013-02-28 20:40:48
        [sent] => 2013-02-28 20:40:48
    )
    [1] => Array (
        [0] => 5253758
        [id] => 5253758
        [1] => 2010-11-16 12:56:39
        [sent] => 2010-11-16 12:56:39
    )
    [2] => Array (
        [0] => 5253517
        [id] => 5253517
        [1] => 2010-11-16 12:43:57
        [sent] => 2010-11-16 12:43:57
    )
    [3] => Array (
        [0] => 5252348
        [id] => 5252348
        [1] => 2010-11-16 11:19:35
        [sent] => 2010-11-16 11:19:35
    )
    ...
) 

Is there a way to create a new single array containing only the 'id' values? this is:

array(5255071, 5253758, 5253517, 5252348, ...)

The idea is to avoid using a foreach loop.


Solution

  • You could use array_map() for this:

    function extractId ($array) { return $array['id']; }
    $targetArray = array_map($sourceArray, "extractId");