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
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, 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
<?php// 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
)
Example #2 Get the column of last names from a recordset, indexed by the "id" column
<?php// 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
)
Example #3 Get the column of usernames from the public "username" property of an object
If
__isset()
is not provided, then an empty array will be returned.
PHP / array_combine — DevDocs
array_combine
(PHP 5, PHP 7, PHP 8)
array_combine
—
Creates an array by using one array for keys and another for its values
Description
array_combine(array$keys,array$values):array
Creates an
array
by using the values from the
keys
array as keys and the values from the
values
array as the corresponding values.
Parameters
keys
Array of keys to be used. Illegal values for key will be converted to
string
.
values
Array
of values to be used
Return Values
Returns the combined
array
.
Errors/Exceptions
As of PHP 8.0.0, a
ValueError
is thrown if the number of elements in
keys
and
values
does not match. Prior to PHP 8.0.0, a
E_WARNING
was emitted instead.
Changelog
Version
Description
8.0.0
array_combine()
will now throw a
ValueError
if the number of elements for each array is not equal; previously this function returned
false
instead.
Multiple occurrences in
$array1
are all treated the same way. This will output :
Array
(
[1] => blue
)
Example #2
array_diff()
example with non-matching types
Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2
. That is, when the string representation is the same.
<?php// This will generate a Notice that an array cannot be cast to a string.$source=[1,2,3,4];$filter=[3,4,[5],6];$result=array_diff($source,$filter);// Whereas this is fine, since the objects can cast to a string.classS{private$v;publicfunction__construct(string$v){$this->v=$v;}publicfunction__toString(){return$this->v;}}$source=[newS('a'),newS('b'),newS('c')];$filter=[newS('b'),newS('c'),newS('d')];$result=array_diff($source,$filter);// $result now contains one instance of S('a');?>
To use an alternate comparison function, see
array_udiff()
.
Notes
Note
:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using
array_diff($array1[0], $array2[0]);
.
See Also
array_diff_assoc() - Computes the difference of arrays with additional index check
array_udiff() - Computes the difference of arrays by using a callback function for data comparison
array_intersect() - Computes the intersection of arrays
array_intersect_assoc() - Computes the intersection of arrays with additional index check
Compares
array
against
arrays
and returns the difference. Unlike
array_diff()
the array keys are also used in the comparison.
Parameters
array
The array to compare from
arrays
Arrays to compare against
Return Values
Returns an
array
containing all the values from
array
that are not present in any of the other arrays.
Changelog
Version
Description
8.0.0
This function can now be called with only one parameter. Formerly, at least two parameters have been required.
Examples
Example #1
array_diff_assoc()
example
In this example you see the
"a" => "green"
pair is present in both arrays and thus it is not in the output from the function. Unlike this, the pair
0 => "red"
is in the output because in the second argument
"red"
has key which is
1
.
Two values from
key => value
pairs are considered equal only if
(string) $elem1 === (string)
$elem2
. In other words a strict check takes place so the string representations must be the same.
Note
:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example,
array_diff_assoc($array1[0], $array2[0]);
.
Note
:
Ensure you pass arguments in the correct order when comparing similar arrays with more keys. The new array should be the first in the list.
See Also
array_diff() - Computes the difference of arrays
array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
array_intersect() - Computes the intersection of arrays
array_intersect_assoc() - Computes the intersection of arrays with additional index check
array_diff_key
—
Computes the difference of arrays using keys for comparison
Description
array_diff_key(array$array,array...$arrays):array
Compares the keys from
array
against the keys from
arrays
and returns the difference. This function is like
array_diff()
except the comparison is done on the keys instead of the values.
Parameters
array
The array to compare from
arrays
Arrays to compare against
Return Values
Returns an
array
containing all the entries from
array
whose keys are absent from all of the other arrays.
Changelog
Version
Description
8.0.0
This function can now be called with only one parameter. Formerly, at least two parameters have been required.
Examples
Example #1
array_diff_key()
example
The two keys from the
key => value
pairs are considered equal only if
(string) $key1 === (string) $key2
. In other words a strict type check is executed so the string representation must be the same.
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using
array_diff_key($array1[0], $array2[0]);
.
See Also
array_diff() - Computes the difference of arrays
array_udiff() - Computes the difference of arrays by using a callback function for data comparison
array_diff_assoc() - Computes the difference of arrays with additional index check
array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
array_diff_ukey() - Computes the difference of arrays using a callback function on the keys for comparison
array_intersect() - Computes the intersection of arrays
array_intersect_assoc() - Computes the intersection of arrays with additional index check
array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
array_intersect_key() - Computes the intersection of arrays using keys for comparison
array_intersect_ukey() - Computes the intersection of arrays using a callback function on the keys for comparison