Organizational Research By

Surprising Reserch Topic

array_column — Return the values from a single column in the input array


array array_column ( array $array , mixed $column_key [, mixed $index_key = null ] )

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_keycolumn in the input array.


asked May 25, 2015 in PHP by rahulgii
0 votes
33 views



Related Hot Questions

2 Answers

0 votes
Get column of first names from recordset

// Array representing a possible record set returned from a database
$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);
?>
The above example will output:

Array
(
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
)
answered May 25, 2015 by rahulgii
0 votes
Get column of last names from recordset, indexed by the "id" column

// Using the $records array from Example #1
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>
The above example will output:

Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)
answered May 25, 2015 by rahulgii

...