PHP


Find the location of an Item in the multidimensional array

Coding Issue for android php java html-css and javascript

Sometimes we have a large multi-dimensional array and we need only one tree of an array where a specific keyword or specific element are exiting. So in this situation, we need to find the location of an Item in the multidimensional array or something like that.

That’s why now I am going to tell you a specific function to return an array of matched item from the top level to end.

We will use the array_merge PHP function to merge an array in every step of an array.

Iterative Approach:

This searching criterion depends upon iterating over the array. Searching for a significant match is the simplest approach one can follow.

Check if an element of the given array is itself an array or not and add the element to the search path, else run array search on the nested array.

For each steps a loop will work with function call itself, for example:

In case, when levels of nested arrays increase, it becomes hard to write such programs and debug them. In such cases, it’s better to write a recursive program which can clearly be written without adding any nested for loops.

This is also important for you:

Explode string by the last occurrence and get the first part

Now create an multi-dimensional array example:

And now if we try to find tree by the name “Hulk” then return array will be like this:

And if we try to find tree by the name “Loki” then return array will be like this and find the location of an Item in the multidimensional array :

So, Final code is:

Published by Amit Pandey

Professional Android app Developer, Full Stack Web Developer, Blogger, Entrepreneur And Founder of Coding Issue. I am always trying to learn new things or new possible ways to do things.

Did you find this page helpful?

X

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*