For testing purposes, I have to create arrays with a given depth.
Is there a simple way to create them?
Edit : sorry for the too broad question, my bad.
In order to test a method which tells the depth of a given array, the depth being the maximum number of dimensions an array can have, I need a function which can create an array with a given depth.
I'd want for instance a function whose result would be something like this, for a depth value of 3 :
Array
(
[0] => Array
(
[0] => Array
(
)
)
)
Just do a recursive function like this:
<?php
$a = array();
function add_level($a, $nb) {
$to_add = array();
if ($nb -- > 0) {
$to_add = add_level($to_add, $nb);
}
$a['child'] = $to_add;
return $a;
}
print_r(add_level($a, 5));
?>
Output is
Array
(
[child] => Array
(
[child] => Array
(
[child] => Array
(
[child] => Array
(
[child] => Array
(
[child] => Array
(
)
)
)
)
)
)
)