Manipulating Elements of a Multidimensional Array in PHP
Let's do something with the iterable
elements of the array, for example, append
the sign '!' to them:
<?php
function func($arr) {
$length = count($arr);
for ($i = 0; $i < $length; $i++) {
if (is_array($arr[$i])) {
$arr[$i] = func($arr[$i]);
} else {
$arr[$i] = $arr[$i] . '!';
}
}
return $arr;
}
var_dump(func([1, [2, 7, 8], [3, 4, [5, 6]]]));
?>
Given a multidimensional array of arbitrary nesting level, for example, like this:
<?php
$arr = [1, [2, 7, 8], [3, 4], [5, [6, 7]]];
?>
Square all numeric elements of this array.