This is one stop global knowledge base where you can learn about all the products, solutions and support features.
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
array_diff_ukey — Computes the difference of arrays using a callback function on the keys for comparison
array_diff_ukey(array $array, array ...$arrays, callable $key_compare_func): 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.
Unlike array_diff_key() a user supplied callback function is used for the indices comparison, not internal function.
array
The array to compare from
arrays
Arrays to compare 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
Returns an
array
containing all the entries from
array
that are not present in any of the other arrays.
Example #1 array_diff_ukey() example
<?php
function key_compare_func($key1, $key2)
{
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_diff_ukey($array1, $array2, 'key_compare_func'));
?>
The above example will output:
array(2) { ["red"]=> int(2) ["purple"]=> int(4) }
Note :
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using
array_diff_ukey($array1[0], $array2[0], 'callback_func');
.
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
array_fill — Fill an array with values
array_fill(int $start_index, int $count, mixed $value): array
Fills an array with
count
entries of the value of the
value
parameter, keys starting at the
start_index
parameter.
start_index
The first index of the returned array.
If
start_index
is negative, the first index of the returned array will be
start_index
and the following indices will start from zero prior to PHP 8.0.0; as of PHP 8.0.0, negative keys are incremented normally (see example).
count
Number of elements to insert. Must be greater than or equal to zero, and less than or equal to
2147483647
.
value
Value to use for filling
Returns the filled array
Throws a
ValueError
if
count
is out of range.
Version | Description |
---|---|
8.0.0 |
array_fill()
now throws a
ValueError
if
count
is out of range; previously
E_WARNING
was raised, and the function returned
false
.
|
Example #1 array_fill() example
<?php
$a = array_fill(5, 6, 'banana');
print_r($a);
?>
The above example will output:
Array ( [5] => banana [6] => banana [7] => banana [8] => banana [9] => banana [10] => banana )
Example #2 array_fill() example with a negative start index
<?php
$a = array_fill(-2, 4, 'pear');
print_r($a);
?>
Output of the above example in PHP 7:
Array ( [-2] => pear [0] => pear [1] => pear [2] => pear )
Output of the above example in PHP 8:
Array ( [-2] => pear [-1] => pear [0] => pear [1] => pear )
Note that index
-1
is not present prior to PHP 8.0.0.
See also the Arrays section of manual for a detailed explanation of negative keys.
(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.