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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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