So I'm trying to write a function that returns a nested array from a nested index in a multidimensional array & I'm having problems...

The array example:

$Array = Array
(
    [1] => Array
        (
            [id] => 1
            [parent] => 0
        )

[5] => Array
    (
        [id] => 5
        [parent] => 0
        [children] => Array
            (
                [7] => Array
                    (
                        [id] => 7
                        [parent] => 5
                        [children] => Array
                            (
                                [9] => Array
                                    (
                                        [id] => 9
                                        [parent] => 7
                                    )

                                [12] => Array
                                    (
                                        [id] => 12
                                        [parent] => 7
                                    )

                            )

                    )

            )

    )

[2] => Array
    (
        [id] => 2
        [parent] => 0
        [children] => Array
            (
                [3] => Array
                    (
                        [id] => 3
                        [parent] => 2
                    )

            )

    )

)

What I want to do is create a function that takes an int and returns the children for that index.

For example, I could pass 7 and get back the array in:

$Array[5]['children'][7]['children']

or pass 5 and get just:

$Array[5]['children']

i've gotten as far as this function:

function searchArray($int, $Array, $strict=false, $path=array()) {

if( !is_array($Array) ) {
	return false;
}

foreach( $Array as $key => $val ) {

	if(is_array($val) && $subPath = searchArray($int, $val, $strict, $path) ) {

		$path = array_merge($path, array($key), $subPath);
		return $path;

	} elseif ((!$strict && $val == $int) || ($strict && $val === $int) ) {

		$path[] = $key;
		return $path;

	}
}

return false;
}

$NewArray = searchArray(7, $Array);

But it just returns this, which may be helpful, I'm just not sure how to iterate it into the result I'm looking for as stated above:

$NewArray = Array
(
    [0] => 5
    [1] => children
    [2] => 7
    [3] => id
)

One option I'm exploring is running this loop on the "NewArray" above:

$str = NULL;

foreach ($NewArray as $key => $var) {

if(is_integer($var)) $var = "[".$var."]";

else $var = "['".$var."']";

$var = str_ireplace('id', 'children', $var);

$str .= $var;

}

$strfinal = '$'."Array$str";

Which returns this string:

$Array[5]['children'][7]['children']

That string is exactly the nested array I want to access, but I can't convert it to a var -

I tried a variable variable:

$FinalArray = $$strfinal;

And also eval().

Neither work.

So my 1st question is, does anyone have a better method for accessing one of the nested arrays in my Array example by index only?

And if not, my 2nd question is, does anyone know how to turn a string like

$Array[5]['children'][7]['children']

into it's actual value?

Thanks!

    Ok, took a nap & solved my own problem...

    Forget about all the other stuff about turning a string into a var, etc.

    Wrote this function to run on my original array and it works exactly as I want:

    function arrayGetNestedByInt($int, $Array) {
    
    foreach ($Array as $key => $val) {
    
    	if(array_key_exists('children', $Array[$key])) {
    
    		if($key == $int) return $Array[$key]['children'];
    
    		else return arrayGetNestedByInt($int, $Array[$key]['children']);
    
    	}
    
    }
    
    }
    

    It's so stupidly obvious I can't believe it took me this long to get there. Sheesh, I need to go to bed earlier at night...

      Write a Reply...