- PHP array_column() Function
- Definition and Usage
- Syntax
- Parameter Values
- Technical Details
- More Examples
- Example
- array_column
- Parameters
- Return Values
- Changelog
- Examples
- Request for Comments: array_column
- Specification
- Description
- Parameters
- Return Values
- Examples
- Example #1: Get column of first names from recordset
- Example #2: Retrieve a column of values from a numerically-indexed array
- Example #3: Get column of last names from recordset, indexed by the «id» column
- Example #4: Mismatched columns
- Example #5: indexKey Collisions
- Proposal and Patch
- Mailing list discussion
- Voting
PHP array_column() Function
// An array that represents a possible record set returned from a database
$a = array(
array(
‘id’ => 5698,
‘first_name’ => ‘Peter’,
‘last_name’ => ‘Griffin’,
),
array(
‘id’ => 4767,
‘first_name’ => ‘Ben’,
‘last_name’ => ‘Smith’,
),
array(
‘id’ => 3809,
‘first_name’ => ‘Joe’,
‘last_name’ => ‘Doe’,
)
);
?php
$last_names = array_column($a, ‘last_name’);
print_r($last_names);
?>
Definition and Usage
The array_column() function returns the values from a single column in the input array.
Syntax
Parameter Values
Parameter | Description |
---|---|
array | Required. Specifies the multi-dimensional array (record-set) to use. As of PHP 7.0, this can also be an array of objects. |
column_key | Required. An integer key or a string key name of the column of values to return. This parameter can also be NULL to return complete arrays (useful together with index_key to re-index the array) |
index_key | Optional. The column to use as the index/keys for the returned array |
Technical Details
Return Value: | Returns an array of values that represents a single column from the input array |
---|---|
PHP Version: | 5.5+ |
More Examples
Example
Get column of last names from a recordset, indexed by the «id» column:
// An array that represents a possible record set returned from a database
$a = array(
array(
‘id’ => 5698,
‘first_name’ => ‘Peter’,
‘last_name’ => ‘Griffin’,
),
array(
‘id’ => 4767,
‘first_name’ => ‘Ben’,
‘last_name’ => ‘Smith’,
),
array(
‘id’ => 3809,
‘first_name’ => ‘Joe’,
‘last_name’ => ‘Doe’,
)
);
?php
$last_names = array_column($a, ‘last_name’, ‘id’);
print_r($last_names);
?>
array_column
array_column() returns the values from a single column of the array , identified by the column_key . Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.
Parameters
A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is provided, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the __get() and __isset() magic methods.
The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be null to return complete arrays or objects (this is useful together with index_key to reindex the array).
The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name. The value is cast as usual for array keys (however, prior to PHP 8.0.0, objects supporting conversion to string were also allowed).
Return Values
Returns an array of values representing a single column from the input array.
Changelog
Version | Description |
---|---|
8.0.0 | Objects in columns indicated by index_key parameter will no longer be cast to string and will now throw a TypeError instead. |
Examples
Example #1 Get the column of first names from a 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’ ,
)
);
?php
$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 )
Example #2 Get the column of last names from a 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 );
?>?php
The above example will output:
Array ( [2135] => Doe [3245] => Smith [5342] => Jones [5623] => Doe )
Example #3 Get the column of usernames from the public «username» property of an object
class User
public $username ;
public function __construct ( string $username )
$this -> username = $username ;
>
>
$users = [
new User ( ‘user 1’ ),
new User ( ‘user 2’ ),
new User ( ‘user 3’ ),
];
print_r ( array_column ( $users , ‘username’ ));
?>
The above example will output:
Array ( [0] => user 1 [1] => user 2 [2] => user 3 )
Example #4 Get the column of names from the private «name» property of an object using the magic __get() method.
class Person
private $name ;
public function __construct ( string $name )
$this -> name = $name ;
>
public function __get ( $prop )
return $this -> $prop ;
>
public function __isset ( $prop ) : bool
return isset( $this -> $prop );
>
>
$people = [
new Person ( ‘Fred’ ),
new Person ( ‘Jane’ ),
new Person ( ‘John’ ),
];
print_r ( array_column ( $people , ‘name’ ));
?>
The above example will output:
Array ( [0] => Fred [1] => Jane [2] => John )
- Array Functions
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_is_list
- array_key_exists
- array_key_first
- array_key_last
- array_keys
- array_map
- array_merge_recursive
- array_merge
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace_recursive
- array_replace
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff_assoc
- array_udiff_uassoc
- array_udiff
- array_uintersect_assoc
- array_uintersect_uassoc
- array_uintersect
- array_unique
- array_unshift
- array_values
- array_walk_recursive
- array_walk
- array
- arsort
- asort
- compact
- count
- current
- end
- extract
- in_array
- key_exists
- key
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort
- each
Request for Comments: array_column
This RFC proposes a new array function that returns the values of the specified column from a multi-dimensional array. Inspired by database methods like PDOStatement::fetchColumn() , array_column() moves useful functionality into the core that once had to be implemented in userland code with sometimes complex loops.
This has been submitted as a pull request on GitHub, where there has already been a significant amount of discussion.
Specification
Description
array array_column(array $input, mixed $columnKey[, mixed $indexKey])
array_column() returns the values from a single column of the input array, identified by the columnKey. Optionally, you may provide an indexKey to index the values in the returned array by the values from the indexKey column in the input array.
Parameters
The column of values to return. This value may be the integer key of the column you wish to retrieve, or it may be the string key name for an associative array.
(Optional.) The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name.
Return Values
Returns an array of values representing a single column from the input array.
Examples
Example #1: 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' ) ); $firstNames = array_column($records, 'first_name'); print_r($firstNames);
The above example will output:
Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter )
Example #2: Retrieve a column of values from a numerically-indexed array
$records = array( array(1, 'John', 'Doe'), array(2, 'Sally', 'Smith'), array(3, 'Jane', 'Jones') ); $lastNames = array_column($records, 2); print_r($lastNames);
The above example will output:
Array ( [0] => Doe [1] => Smith [2] => Jones )
Example #3: Get column of last names from recordset, indexed by the «id» column
The above example will output:
Array ( [2135] => Doe [3245] => Smith [5342] => Jones [5623] => Doe )
Example #4: Mismatched columns
With array_column() the relationship in finding the values of columnKey to indexKey is much like that of a SQL left join. All values of the columnKey are returned. When a corresponding indexKey cannot be found, the value will be keyed with an integer, starting from zero.
The following examples will all use the same $mismatchedColumns array defined here:
$mismatchedColumns = array( array( 'a' => 'foo', 'b' => 'bar', 'e' => 'baz' ), array( 'a' => 'qux', 'c' => 'quux', 'd' => 'corge' ), array( 'a' => 'grault', 'b' => 'garply', 'e' => 'waldo' ), );
In this example, all rows contain an “a” key, but only two contain a “b” key. If we want to retrieve all “a” values and key them by “b” values, then array_column() behaves like this:
$foo = array_column($mismatchedColumns, 'a', 'b'); $bar = array('bar' => 'foo', 'qux', 'garply' => 'grault'); /* Both $foo and $bar contain values that look like this: Array ( [bar] => foo [0] => qux [garply] => grault ) */
However, if we want to retrieve all “b” values and key them by “a” values, we will only have two elements in the resulting array, since only two rows contain “b” values.
// There is a corresponding "a" value for each "b" value print_r(array_column($mismatchedColumns, 'b', 'a')); /* Array ( [foo] => bar [grault] => garply ) */ // There are no corresponding "c" values for either "b" value print_r(array_column($mismatchedColumns, 'b', 'c')); /* Array ( [0] => bar [1] => garply ) */
Example #5: indexKey Collisions
In the event that more than one row contains the same value for indexKey, then the last columnKey value for that indexKey will overwrite the previous value.
// Using the $records array from Example #1 $firstNames = array_column($records, 'first_name', 'last_name'); print_r($firstNames); /* Array ( [Doe] => Peter [Smith] => Sally [Jones] => Jane ) */
Proposal and Patch
The patch (including tests) for this proposal is available in GitHub Pull Request #257.
Mailing list discussion
The mailing list discussion is available here.
Voting
Voting ends not before Friday, January 18, 2013. The PHP language is not changed, so a 50% + 1 majority is required.