3

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

5 Answers 5

10

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

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

http://php.net/array_column

Sign up to request clarification or add additional context in comments.

4 Comments

I'm running PHP 5.5.34-0ubuntu0.13.04.1. getting error "PHP Fatal error: Call to undefined function array_column()"
Then I suspect you're not actually running the php version you think you're running. If you're running your code through a webserver page, try to check your php version the same way (using a phpinfo() page)
Alternatively, if you're worried about BC, check the other answers for your question, all of them might do the trick.
You're right, I was running PHP 5.4.9...not sure why Ubuntu packaged it like that. I've upgrade to PHP 5.5 now and array_column is working as expected. Thanks!
3

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.

1 Comment

Beat me to it. I was about to add that to my answer.
2

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

Comments

2

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
)

Comments

1
$simple = [];

foreach ($stores as $store){
    $simple[] = $store['id'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.