0

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;.

13
  • 1
    Possible duplicate of Checking during array iteration, if the current element is the last element Commented Nov 25, 2019 at 21:59
  • @PatrickQ Nope. That question is asking how to find the last element in a single-dimensional array. I am asking how to find the last array in a multi-dimensional array. Commented Nov 25, 2019 at 23:11
  • 1
    Please always provide array/object data from var_export() or as a json encoded string. I find your input and desired output to be Unclear. Commented Nov 26, 2019 at 4:28
  • @AlexanderGR The logic is the same. As you noted, you already have code that iterates through your multi-dimensional array. You just apply the check at each level. A multi-dimensional array is just an array of (at some point) single-dimensional arrays. Commented Nov 26, 2019 at 12:52
  • @PatrickQ The goal is not at all finding the elements of an array. Finding the last element will get each and every end of an array, but I want to get ONLY the end of an array of arrays. As posed in the question, it should only get every line where final array*** is written. Think of the question more to do with the PHP array() function and counting the instances of using said function. Commented Nov 26, 2019 at 16:44

1 Answer 1

1

You can use array_key_last() to find the key of the final element in an array before iterating it. This can be applied to any number of array dimensions.

This example will output the text for each node in your example tree and indicate whether that node is the final array element.

function listElements(array $input)
{
    $final = array_key_last($input);
    $n = count($input);
    foreach ($input as $key => $element) {
        echo $element['text'], ($key == $final) ? " last of $n" : '', PHP_EOL;
        if (isset($element['children'])) {
            listElements($element['children']);
        }
    }
}

(This uses the same approach shown in the most recent update of this answer to the proposed duplicate. I'm just showing here how it can still work when applied recursively.)


Based on your edit it looks like the $i you're referring to corresponds to recursion depth. You can keep track of that by adding the depth to the function signature and incrementing it in the recursive call. Here is an example using PHP 5.6 syntax with end()/key() rather than array_key_last().

function listElements(array $input, $depth = 0)
{
    end($input);
    $final = key($input);
    $n = count($input);
    foreach ($input as $key => $element) {
        echo $element['text'];
        echo ($key == $final) ? " last of $n," : '';
        echo " depth $depth", PHP_EOL;
        if (isset($element['children'])) {
            listElements($element['children'], $depth + 1);
        }
    }
}

Working example at https://3v4l.org/J9fek

Sign up to request clarification or add additional context in comments.

9 Comments

This is useful and functionally solves the problem, but it doesn't actually count each Array.
Ah, I incorrectly assumed the count was only there because you were thinking it was needed to determine the last element. I can edit the answer to suggest a way to add a count
Okay, updated to include count. I haven't tested the updated code, but I think it should work okay.
This is still counting the elements. Is there a way to count the instances of the PHP array() function itself?
But every element here is an array, right? Sorry, I think I'm still not understanding exactly what you're asking. Can you show a small example array and what the count should be for it? The example in your question clearly shows what you meant by final array, but doesn't really address the count aspect unless I missed something. Which is always possible ;-)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.