I have a multi-dimensional array that looks something like the following:
array (
0 => array ( 'text' => 'LEVEL 0-0', 'children' =>
array (
0 => array ( 'text' => 'LEVEL 1-0 0', 'children' =>
array (
0 => array ( 'text' => 'LEVEL 2-0 0', 'children' =>
array (
0 => array ( 'text' => 'LEVEL 3-0 0', ),
1 => array ( 'text' => 'LEVEL 3-1 0', ),
2 => array ( 'text' => 'LEVEL 3-2 0', ),
3 => array ( 'text' => 'LEVEL 3-3 0', ),
4 => array ( 'text' => 'LEVEL 3-4 0', ),
5 => array ( 'text' => 'LEVEL 3-5 0', ), ), ),
1 => array ( 'text' => 'LEVEL 2-1 0', ),
2 => array ( 'text' => 'LEVEL 2-2 0', 'children' =>
array (
0 => array ( 'text' => 'LEVEL 3-0 2', ), ), ),
3 => array ( 'text' => 'LEVEL 2-3 0', ),
4 => array ( 'text' => 'LEVEL 2-4 0', ),
5 => array ( 'text' => 'LEVEL 2-5 0', 'children' =>
array (
0 => array ( 'text' => 'LEVEL 3-0 5', ),
1 => array ( 'text' => 'LEVEL 3-1 5', ),
2 => array ( 'text' => 'LEVEL 3-2 5', ),
3 => array ( 'text' => 'LEVEL 3-3 5', ),
4 => array ( 'text' => 'LEVEL 3-4 5', ),
5 => array ( 'text' => 'LEVEL 3-5 5', ), ), ),
6 => array ( 'text' => 'LEVEL 2-6 0', ),
7 => array ( 'text' => 'LEVEL 2-7 0', ), ), ),
),
),
)
How can I count and check if an array is the final array in an array?
Let's say we have a counter $i = 0;
.
Whenever an array has children, we want the the counter to increment.
if (!empty($value['children'])) { $i++; }
Whenever we reach the final array in an array, we want the counter to decrement.
pseudo-code if (final array in array) { $i--; }
Our ending count should be $i = 0;
.