PHP Store Key Value from Associative Array into Simple Array

后端 未结 5 2036
半阙折子戏
半阙折子戏 2021-01-21 07:57

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
(         


        
相关标签:
5条回答
  • 2021-01-21 08:08

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

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

    http://php.net/array_column

    0 讨论(0)
  • 2021-01-21 08:16

    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.

    0 讨论(0)
  • 2021-01-21 08:16

    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
    )
    
    0 讨论(0)
  • 2021-01-21 08:24

    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)
    }
    
    0 讨论(0)
  • 2021-01-21 08:25
    $simple = [];
    
    foreach ($stores as $store){
        $simple[] = $store['id'];
    }
    
    0 讨论(0)
提交回复
热议问题