Map, Walk
Apply a user function to every member of an array.
/**
* array_map, array_walk()
*
* Array walk needs reference
*/
function cube($n)
{
return pow($n, 3);
}
$A = [1, 2, 3];
$B = [1, 2, 3];
$C = [1, 2, 3];
$D = [1, 2, 3];
$A = array_map('cube', $A);
$B = array_map(fn($x) => pow($x, 3), $B);
array_walk($C, fn(&$x) => $x = pow($x, 3));
array_walk($D, fn(&$x) => $x *= 2); // Look Here
print_r($A); // 1 8 27
print_r($B); // 1 8 27
print_r($C); // 1 8 27
print_r($D); // 2 4 6
Next
We can use internal pointer when working with arrays.
/**
* Reset, key, next, current ...
*/
$arr = ['a'=>1, 'b'=>2, 'c'=>3];
reset($arr);
while (key($arr) !== NULL) {
print_r( key($arr) . current($arr) . " " ); // a1 b2 c3
next($arr);
}
Exists
Check to see if array exists or not.
/**
* Check if array exists
* Use is_array() / not count()
*/
$a = array();
echo count($a); // 0
echo is_array($a); // true
Last update: 451 days ago