array_intersect_assoc()
returns an array containing all the values of
array
that are present in all the arguments. Note that the keys are also used in the comparison unlike in
array_intersect()
.
Parameters
array
The array with master values to check.
arrays
Arrays to compare values against.
Return Values
Returns an associative array containing all the values in
array
that are present in all of the arguments.
Changelog
Version
Description
8.0.0
This function can now be called with only one parameter. Formerly, at least two parameters have been required.
In our example you see that only the pair
"a" =>
"green"
is present in both arrays and thus is returned. The value
"red"
is not returned because in
$array1
its key is
0
while the key of "red" in
$array2
is
1
, and the key
"b"
is not returned because its values are different in each array.
The two values from the
key => value
pairs are considered equal only if
(string) $elem1 === (string) $elem2
. In other words a strict type check is executed so the string representation must be the same.
See Also
array_intersect() - Computes the intersection of arrays
array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
array_diff() - Computes the difference of arrays
array_diff_assoc() - Computes the difference of arrays with additional index check
PHP / array_intersect_key — DevDocs
array_intersect_key
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
array_intersect_key
—
Computes the intersection of arrays using keys for comparison
In our example you see that only the keys
'blue'
and
'green'
are present in both arrays and thus returned. Also notice that the values for the keys
'blue'
and
'green'
differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of
array
.
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.
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_key() - Computes the difference of arrays using keys for comparison
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_ukey() - Computes the intersection of arrays using a callback function on the keys for comparison
array_intersect_uassoc()
returns an array containing all the values of
array
that are present in all the arguments. Note that the keys are used in the comparison unlike in
array_intersect()
.
Parameters
array
Initial array for comparison of the arrays.
arrays
Arrays to compare keys against.
key_compare_func
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
callback(mixed$a,mixed$b):int
Return Values
Returns the values of
array
whose values exist in all of the arguments.
array_intersect_ukey()
returns an array containing all the values of
array
which have matching keys that are present in all the arguments.
Parameters
array
Initial array for comparison of the arrays.
arrays
Arrays to compare keys against.
key_compare_func
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
callback(mixed$a,mixed$b):int
Return Values
Returns the values of
array
whose keys exist in all the arguments.
In our example you see that only the keys
'blue'
and
'green'
are present in both arrays and thus returned. Also notice that the values for the keys
'blue'
and
'green'
differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of
array
.
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_key() - Computes the difference of arrays using keys for comparison
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_is_list
—
Checks whether a given
array
is a list
Description
array_is_list(array$array):bool
Determines if the given
array
is a list. An
array
is considered a list if its keys consist of consecutive numbers from
0
to
count($array)-1
.
Parameters
array
The
array
being evaluated.
Return Values
Returns
true
if
array
is a list,
false
otherwise.
Examples
Example #1
array_is_list()
example
<?phparray_is_list([]);// truearray_is_list(['apple',2,3]);// truearray_is_list([0=>'apple','orange']);// true// The array does not start at 0array_is_list([1=>'apple','orange']);// false// The keys are not in the correct orderarray_is_list([1=>'apple',0=>'orange']);// false// Non-integer keysarray_is_list([0=>'apple','foo'=>'bar']);// false// Non-consecutive keysarray_is_list([0=>'apple',2=>'bar']);// false?>
Notes
Note
:
This function returns
true
on empty arrays.
See Also
array_values() - Return all the values of an array
For backward compatibility reasons,
array_key_exists()
will also return
true
if
key
is a property defined within an
object
given as
array
. This behaviour is deprecated as of PHP 7.4.0, and removed as of PHP 8.0.0.
To check whether a property exists in an object,
property_exists()
should be used.
See Also
isset() - Determine if a variable is declared and is different than null
array_keys() - Return all the keys or a subset of the keys of an array
in_array() - Checks if a value exists in an array
property_exists() - Checks if the object or class has a property