This is one stop global knowledge base where you can learn about all the products, solutions and support features.
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
array_fill_keys — Fill an array with values, specifying keys
array_fill_keys(array $keys, mixed $value): array
Fills an array with the value of the
value
parameter, using the values of the
keys
array as keys.
keys
Array of values that will be used as keys. Illegal values for key will be converted to string .
value
Value to use for filling
Returns the filled array
Example #1 array_fill_keys() example
<?php
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a);
?>
The above example will output:
Array ( [foo] => banana [5] => banana [10] => banana [bar] => banana )
(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)
array_filter — Filters elements of an array using a callback function
array_filter(array $array, ?callable $callback = null, int $mode = 0): array
Iterates over each value in the
array
passing them to the
callback
function. If the
callback
function returns
true
, the current value from
array
is returned into the result
array
.
Array keys are preserved, and may result in gaps if the
array
was indexed. The result
array
can be reindexed using the
array_values()
function.
array
The array to iterate over
callback
The callback function to use
If no
callback
is supplied, all empty entries of
array
will be removed. See
empty()
for how PHP defines empty in this case.
mode
Flag determining what arguments are sent to
callback
:
ARRAY_FILTER_USE_KEY
- pass key as the only argument to
callback
instead of the value
ARRAY_FILTER_USE_BOTH
- pass both value and key as arguments to
callback
instead of the value
0
which will pass value as the only argument to
callback
instead.
Returns the filtered array.
Version | Description |
---|---|
8.0.0 |
callback
is nullable now.
|
8.0.0 |
If
callback
expects a parameter to be passed by reference, this function will now emit an
E_WARNING
.
|
Example #1 array_filter() example
<?php
function odd($var)
{
// returns whether the input integer is odd
return $var & 1;
}
function even($var)
{
// returns whether the input integer is even
return !($var & 1);
}
$array1 = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
$array2 = [6, 7, 8, 9, 10, 11, 12];
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
?>
The above example will output:
Odd : Array ( [a] => 1 [c] => 3 [e] => 5 ) Even: Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12 )
Example #2
array_filter()
without
callback
<?php
$entry = [
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => '',
5 => '0',
6 => 0,
];
print_r(array_filter($entry));
?>
The above example will output:
Array ( [0] => foo [2] => -1 )
Example #3
array_filter()
with
mode
<?php
$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
var_dump(array_filter($arr, function($k) {
return $k == 'b';
}, ARRAY_FILTER_USE_KEY));
var_dump(array_filter($arr, function($v, $k) {
return $k == 'b' || $v == 4;
}, ARRAY_FILTER_USE_BOTH));
?>
The above example will output:
array(1) { ["b"]=> int(2) } array(2) { ["b"]=> int(2) ["d"]=> int(4) }
If the array is changed from the callback function (e.g. element added, deleted or unset) the behavior of this function is undefined.
(PHP 4, PHP 5, PHP 7, PHP 8)
array_flip — Exchanges all keys with their associated values in an array
array_flip(array $array): array
array_flip()
returns an
array
in flip order, i.e. keys from
array
become values and values from
array
become keys.
Note that the values of
array
need to be valid keys, i.e. they need to be either
int
or
string
. A warning will be emitted if a value has the wrong type, and the key/value pair in question
will not be included in the result
.
If a value has several occurrences, the latest key will be used as its value, and all others will be lost.
array
An array of key/value pairs to be flipped.
Returns the flipped array.
Example #1 array_flip() example
<?php
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);
print_r($flipped);
?>
The above example will output:
Array ( [oranges] => 0 [apples] => 1 [pears] => 2 )
Example #2 array_flip() example : collision
<?php
$input = array("a" => 1, "b" => 1, "c" => 2);
$flipped = array_flip($input);
print_r($flipped);
?>
The above example will output:
Array ( [1] => b [2] => c )
(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)
array_intersect — Computes the intersection of arrays
array_intersect(array $array, array ...$arrays): array
array_intersect()
returns an array containing all the values of
array
that are present in all the arguments. Note that keys are preserved.
array
The array with master values to check.
arrays
Arrays to compare values against.
Returns an array containing all of the values in
array
whose values exist in all of the parameters.
Version | Description |
---|---|
8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
Example #1 array_intersect() example
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>
The above example will output:
Array ( [a] => green [0] => red )
Note : Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2
. In words: when the string representation is the same.
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
array_intersect_assoc — Computes the intersection of arrays with additional index check
array_intersect_assoc(array $array, array ...$arrays): array
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()
.
array
The array with master values to check.
arrays
Arrays to compare values against.
Returns an associative array containing all the values in
array
that are present in all of the arguments.
Version | Description |
---|---|
8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
Example #1 array_intersect_assoc() example
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "b" => "yellow", "blue", "red");
$result_array = array_intersect_assoc($array1, $array2);
print_r($result_array);
?>
The above example will output:
Array ( [a] => green )
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.
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
array_intersect_key — Computes the intersection of arrays using keys for comparison
array_intersect_key(array $array, array ...$arrays): array
array_intersect_key()
returns an array containing all the entries of
array
which have keys that are present in all the arguments.
array
The array with master keys to check.
arrays
Arrays to compare keys against.
Returns an associative array containing all the entries of
array
which have keys that are present in all arguments.
Version | Description |
---|---|
8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
Example #1 array_intersect_key() example
<?php
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_key($array1, $array2));
?>
The above example will output:
array(2) { ["blue"]=> int(1) ["green"]=> int(3) }
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.