Find inner arrays in nested arrays

Posted by 50ndr33 on Stack Overflow See other posts from Stack Overflow or by 50ndr33
Published on 2011-11-19T21:23:51Z Indexed on 2011/11/20 1:50 UTC
Read the original article Hit count: 251

Filed under:
|
|

I have a nested array in PHP:

array (
'0' => "+5x",
'1' => array (
       '0' => "+",
       '1' => "(",
       '2' => "+3",
       '3' => array (
              '0' => "+",
              '1' => "(",
              '2' => array ( // I want to find this one.
                  '0' => "+",
                  '1' => "(",
                  '2' => "+5",
                  '3' => "-3",
                  '4' => ")"
                  ),
              '3' => "-3",
              '4' => ")"
              ),
       '4' => ")"
       )
);       

I need to process the innermost arrays here, the one with the comment: "I want to find this one." Is there a function for that?

I have thought about doing (written as an idea, not as correct PHP):

foreach ($array as $id => $value) {
    if ($value is array) {
        $name = $id;
        foreach ($array[$id] as $id_2 => $value_2) {
            if ($value_2 is array) {
                $name .= "." . $id_2;
                foreach ($array[$id][$id_2] as $id_3 => $value_3) {
                    if ($value_3 is array) {
                        $name .= "." . $id_3;
                        foreach ($array[$id][$id_2][$id_3] as $id_4 => $value_4) {
                            if ($value_4 is array) {
                                $name .= "." . $id_4;
                                foreach [and so it goes on];
                            } else {
                                $listOfInnerArrays[] = $name;
                                break;
                            }
                        }
                    } else {
                        $listOfInnerArrays[] = $name;
                        break;
                    }
                }
            } else {
                $listOfInnerArrays[] = $name;
                break;
            }
        }
    }
}

So what it does is it makes $name the current key in the array. If the value is an array, it goes into it with foreach and adds "." and the id of the array. So we would in the example array end up with:

array (
    '0' => "1.3.2",
)

Then I can process those values to access the innner arrays.

The problem is that the array that I'm trying to find the inner arrays of is dynamic and made of a user input. (It splits an input string where it finds + or -, and puts it in a separate nested array if it contains brackets. So if the user types a lot of brackets, there will be a lot of nested arrays.) Therefore I need to make this pattern go for 20 times down, and still it will only catch 20 nested arrays no matter what.

Is there a function for that, again? Or is there a way to make it do this without my long code? Maybe make a loop make the necessary number of the foreach pattern and run it through eval()?


Long answer to J. Bruni:

<?php

$liste = 

array (
'0' => "+5x",
'1' => array (
       '0' => "+",
       '1' => "(",
       '2' => "+3",
       '3' => array (
              '0' => "+",
              '1' => "(",
              '2' => array (
                  '0' => "+",
                  '1' => "(",
                  '2' => "+5",
                  '3' => "-3",
                  '4' => ")"
                  ),
              '3' => "-3",
              '4' => ")"
              ),
       '4' => ")"
       )
);

function find_deepest( $item, $key )
{
    echo "0";
    if ( !is_array( $item ) )
        return false;

    foreach( $item as $sub_item ) {
        if ( is_array( $sub_item ) )
            return false;
    }
    echo "1";
    print_r( $item );
    return true;
}

array_walk_recursive( $liste, 'find_deepest' );

echo "<pre>";
print_r($liste);

?>

I wrote echo 0 and 1 to see what the script did, and here is the output:

00000000000000

Array
(
    [0] => +5x
    [1] => Array
        (
            [0] => +
            [1] => (
            [2] => +3
            [3] => Array
                (
                    [0] => +
                    [1] => (
                    [2] => Array
                        (
                            [0] => +
                            [1] => (
                            [2] => +5
                            [3] => -3
                            [4] => )
                        )

                    [3] => -3
                    [4] => )
                )

            [4] => )
        )

)

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays