Array
Categories:
array_change_key_case()
array_change_key_case(
array $array
,int $case = CASE_LOWER
):array
Change key cases
Case options
- CASE_UPPER
- CASE_LOWER
function arrayKeyToLowercase(array $array): array
{
return array_change_key_case($array, CASE_LOWER);
}
function arrayKeyToUppercase(array $array): array
{
return array_change_key_case($array, CASE_UPPER);
}
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
Array
(
[FIRST] => 1
[SECOND] => 4
)
array_reverse()
array_reverse(
array $array
,bool $preserve_keys = false
):array
Return an array with elements in reverse order
$input = [
"php",
4.0,
[
"green",
"red"
]
];
$reversed = array_reverse($input);
print_r($input);
// Array
// (
// [0] => php
// [1] => 4
// [2] => Array
// (
// [0] => green
// [1] => red
// )
// )
print_r($reversed);
// Array
// (
// [0] => Array
// (
// [0] => green
// [1] => red
// )
// [1] => 4
// [2] => php
// )
array_map()
array_map(
?callable $callback
,array $array
,array ...$arrays
):array
Applies the callback to the elements
of the given arrays
function cube($n)
{
return ($n * $n * $n);
}
$a = [1, 2, 3, 4, 5];
$b = array_map('cube', $a);
print_r($b);
// Array
// (
// [0] => 1
// [1] => 8
// [2] => 27
// [3] => 64
// [4] => 125
// )
array_diff()
array_diff(
array $array
,array ...$arrays
):array
Computes the difference
of arrays
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
// Array
// (
// [1] => blue
// )
array_diff_assoc()
array_diff_assoc(
array $array
,array ...$arrays
):array
Computes the difference
of arrays with additional index check
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
// Array
// (
// [b] => brown
// [c] => blue
// [0] => red
// )
array_diff_key()
array_diff_key(
array $array
,array ...$arrays
):array
Computes the difference
of arrays using keys
for comparison
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'yellow' => 7, 'cyan' => 8);
var_dump(array_diff_key($array1, $array2));
// array(3) {
// ["blue"]=>
// int(1)
// ["red"]=>
// int(2)
// ["purple"]=>
// int(4)
// }
array_intersect()
array_intersect(
array $array
,array ...$arrays
):array
Computes the intersection
of arrays
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
// Array
// (
// [a] => green
// [0] => red
// )
array_intersect_assoc()
array_intersect_assoc(
array $array
,array ...$arrays
):array
Computes the intersection
of arrays with additional index check
$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);
// Array
// (
// [a] => green
// )
array_intersect_key()
array_intersect_key(
array $array
,array ...$arrays
):array
Computes the intersection
of arrays using keys
for comparison
$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));
// array(2) {
// ["blue"]=>
// int(1)
// ["green"]=>
// int(3)
// }
Array pointer
Function | Description |
---|---|
current() | Return the current element in an array |
end() | Set the internal pointer of an array to its last element |
key() | Fetch a key from an array |
each() | Return the current key and value pair from an array and advance the array |
prev() | Rewind the internal array pointer |
reset() | Set the internal pointer of an array to its first element |
next() | Advance the internal pointer of an array |
current(), prev() and next()
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport); // $mode = 'bike';
$mode = current($transport); // $mode = 'bike';
$mode = prev($transport); // $mode = 'foot';
$mode = end($transport); // $mode = 'plane';
$mode = current($transport); // $mode = 'plane';
$arr = array();
var_dump(current($arr)); // bool(false)
$arr = array(array());
var_dump(current($arr)); // array(0) { }
end()
$fruits = array('apple', 'banana', 'cranberry');
echo end($fruits); // cranberry
key()
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array), "\n";
}
next($array);
}
// fruit1
// fruit4
// fruit5
each()
$foo = array("Robert" => "Bob", "Seppo" => "Sepi");
$bar = each($foo);
print_r($bar);
// Array
// (
// [1] => Bob
// [value] => Bob
// [0] => Robert
// [key] => Robert
// )
reset()
$array = array('step one', 'step two', 'step three', 'step four');
// by default, the pointer is on the first element
echo current($array) . "<br />\n"; // "step one"
// skip two steps
next($array);
next($array);
echo current($array) . "<br />\n"; // "step three"
// reset pointer, start again on step one
reset($array);
echo current($array) . "<br />\n"; // "step one"
Custom Helper function
Explode string and trim value into a new array
function arrayExplodeTrim(
string $separator,
string $string,
int $limit = PHP_INT_MAX
): array {
$explode_array = explode($separator, $string, $limit);
return array_map('trim', $explode_array);
}