Question

I'm having trouble wrapping my head around this, any help would be GREAT...

I have an array $stores that is structured like so:

Array
(
[0] => Array
    (
        [id] => 123
        [name] => 'Store A'
    )

[1] => Array
    (
        [id] => 345
        [name] => 'Store B'
    )

[2] => Array
    (
        [id] => 567
        [name] => 'Store C'
    )

[3] => Array
    (
        [id] => 789
        [name] => 'Store D'
    )
)

I want to extract the 'id' values from this array into a simple array that looks this:

$simple = array(123,345,567,789);
Was it helpful?

Solution

If you use php 5.5+, array_column() is quite useful :

$simple = array_column($yourarray,'id');

http://php.net/array_column

OTHER TIPS

Calimero definitely had the best answer for PHP 5.5+, but if you want the same functionality in prior versions, check this repository: https://github.com/ramsey/array_column . It is written by PHP 5.5 array_column creator itself.

If you can't use array_column, you can use array_map:

$names = array(
    array('id' => 123, 'name' => 'A'),
    array('id' => 456, 'name' => 'B'),
    array('id' => 789, 'name' => 'C'),
);

$ids = array_map(function ($name) {
    return $name['id'];
}, $names);

var_dump($ids);

// output
array(3) {
    [0] => int(123)
    [1] => int(456)
    [2] => int(789)
}

You can simply use the following syntax if you are unable to upgrade the php version. In that kind of case use if (!function_exists('array_column')) to prevent re-declaration of the function which may occur on version upgrade.

Description From php.net

array_column() returns the values from a single column of the array, identified by the column_key. Optionally, you may provide an index_key to index the values in the returned array by the values from the index_key column in the input array.

/* Function array_column equivalent to php's array_column */
if (!function_exists('array_column'))
{

    function array_column(array $array, $column_key, $index_key = NULL)
    {
        if (isset($array))
        {
            $return = array();
            foreach ($array as $a)
            {
                if ($index_key)
                {
                    if (!isset($a[$index_key]))
                    {
                        return array();
                    }
                    else
                    {
                        $return[$a[$index_key]] = $a[$column_key];
                    }
                }
                else
                {
                    $return[] = $a[$column_key];
                }
            }
            return $return;
        }
        return array();
    }
}

Here are some examples taken from PHP.NET

<?php
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);

$first_names = array_column($records, 'first_name');
print_r($first_names);
?>

It will give below output:

Array
(
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
)

Get column of last names from recordset, indexed by the "id" column

<?php
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>

It will give you output as below:

Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)
$simple = [];

foreach ($stores as $store){
    $simple[] = $store['id'];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top