Jump to content

Return nested array from nested index in multidimensional array?


antmeeks

Recommended Posts

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:

$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

$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
)

 

Any ideas? Thanks!

Ok so with the "NewArray" above, I'm running this loop:

$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']"

 

So that string is exactly the nested array I want to access, but how do I convert it to a var?

 

I tried a variable variable

$FinalArray = $$strfinal;

But that doesn't work.

 

Also for S&G, I tried eval, but no luck there either.

 

So can I get any pointers on how to convert the string above to a var?

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

 

What's even worse is that no one here even bothered with it... I hope I joined the right forum.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.