array_column
Description
The array_column of Array for PHP return the values from a single column in the input array.
Syntax
array_column( array $array, int|string|null $column_key, int|string|null $index_key = null ): array
Parameters
array
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.
column_key
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).
index_key
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, objects supporting conversion to string are also allowed).
Return
Returns an array of values representing a single column from the input array.
Examples
1 · array column_key
<? $array = [ [ "id" => 2135, "first" => "John", "last" => "Doe" ], [ "id" => 3245, "first" => "Sally", "last" => "Smith" ], [ "id" => 5342, "first" => "Jane", "last" => "Doe" ], [ "id" => 5623, "first" => "Peter", "last" => "Jones" ] ]; $column_key = "first"; $return = array_column($array, $column_key); print_r($return);
Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter )
2 · index_key
<? $array = [ [ "id" => 2135, "first" => "John", "last" => "Doe" ], [ "id" => 3245, "first" => "Sally", "last" => "Smith" ], [ "id" => 5342, "first" => "Jane", "last" => "Doe" ], [ "id" => 5623, "first" => "Peter", "last" => "Jones" ] ]; $column_key = "first"; $index_key = "id"; $return = array_column($array, $column_key, $index_key); print_r($return);
Array ( [2135] => John [3245] => Sally [5342] => Jane [5623] => Peter )
3 · public
<? class myclass { public $id; public $first; public $last; public function __construct(int $id, string $first, string $last) { $this->id = $id; $this->first = $first; $this->last = $last; } } $array = [ new myclass(2135, "John", "Doe"), new myclass(3245, "Sally", "Smith"), new myclass(5342, "Jane", "Doe"), new myclass(5623, "Peter", "Jones"), ]; $column_key = "first"; $return = array_column($array, $column_key); print_r($return);
Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter )
4 · private
<? class myclass { private $id; private $first; private $last; public function __construct(int $id, string $first, string $last) { $this->id = $id; $this->first = $first; $this->last = $last; } public function __get($prop) { return $this->$prop; } public function __isset($prop) : bool { return isset($this->$prop); } } $array = [ new myclass(2135, "John", "Doe"), new myclass(3245, "Sally", "Smith"), new myclass(5342, "Jane", "Doe"), new myclass(5623, "Peter", "Jones"), ]; $column_key = "first"; $return = array_column($array, $column_key); print_r($return);
Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter )
Links
Array
- array
- array_all
- array_any
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_fill
- array_fill_keys
- array_filter
- array_find
- array_find_key
- array_flip
- array_intersect
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_key_exists
- array_key_first
- array_key_last
- array_keys
- array_map
- array_merge
- array_merge_recursive
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace
- array_replace_recursive
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff
- array_udiff_assoc
- array_udiff_uassoc
- array_uintersect
- array_uintersect_assoc
- array_uintersect_uassoc
- array_unique
- array_unshift
- array_values
- array_walk
- array_walk_recursive
- arsort
- asort
- compact
- count
- current
- end
- extract
- in_array
- key
- key_exists
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort