1

I have an array that can vary in its dimension, sometimes can be small, sometimes can go deeper. I'm trying to search for a particular element in the array and if it is found, I would like to get a specific parent. So for instance, if I have an array as:

Again, the dimension can change, however, I'm looking for the the most outer parent's key that has siblings. (in this case)

Array (
 [0] => Array (
 [0] => Array ( 
 [0] => Array ( <<----------------------------------------+
 [0] => FOO0 | 
 [1] => BAR0 //Search BAR0 returns parent key 0 +
 [2] => Array( |
 [0] => HELLO0 |
 [1] => BYE0 //Search BYE0 returns parent key 0 +
 ) |
 [3] => FOO0 |
 [4] => Array ( |
 [0] => HELLO0 //Search HELLO0 returns parent key 0 --
 )
 )
 [1] => Array ( <<----------------------------------------+
 [0] => FOO1 |
 [1] => BAR1 |
 [2] => Array ( |
 [0] => HELLO1 |
 [1] => BYE1 |
 ) |
 [3] => BASE1 |
 [4] => Array ( |
 [0] => BAZ1 |
 [1] => Array ( | 
 [0] => DOO1 //Search DOO1 returns parent key 1 +
 [1] => BOB2 //Search BOB2 returns parent key 1 +
 )
 )
 )
 [2] => FOO2 // Search FOO2 returns key 2 (itself)
 )
 )
)

Sample Output for FOO2

[2] => FOO2 // searched value

I would really appreciate some help! Thanks!

asked Mar 20, 2011 at 6:17
5
  • What do you mean by key? What is the value you expect to get back? The key would just be an integer like 0 or 1... It wouldn't be very useful from what I can tell. Commented Mar 20, 2011 at 7:55
  • @konforce: If I could get all the child elements of that particular parent key, and at the same time preserving the keys that would be the ideal. Commented Mar 20, 2011 at 15:38
  • Actually the key value would be very useful. Getting the key value would be ideal since I can feed it to another routine. Commented Mar 20, 2011 at 17:47
  • @Tetsuya, could you edit your question and add the exact thing you want returned as an array dump (same format as what's there for the original array) for the queries of HELLO0 and FOO2? Commented Mar 20, 2011 at 17:49
  • @Tetsuya, I think my updated answer is what you are looking for. Commented Mar 20, 2011 at 18:18

2 Answers 2

1

I'm not completely sure this is what you are looking for, but you can give it a try:

function find($needle, array $haystack)
{
 foreach ($haystack as $i => $x) {
 if (is_array($x)) {
 $b = find($needle, $x);
 if ($b) return count($haystack) > 1 ? array($i, $x) : $b;
 }
 else if ($x == $needle) {
 return array($i, $x);
 }
 }
 return false;
}
list($key, $val) = find('FOO1', $data);

This doesn't return the exact element, but a copy of it. If you want the original item, it will need to be updated to use references.

You can change array($i, $x) to array($i => $x) in both places if you don't want to use the list construct when querying the function. But I think it's easier to work with as it is written.

answered Mar 20, 2011 at 6:40
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not quite sure what do you mean with the exact element?
I've changed a bit the post, perhaps it is easier to understand now :)
@Tetsuya, by a "copy" I mean that if you were to modify the $val that is returned, the original $data array will not be changed.
0
function recursive_array_search($k,$h) {
 foreach($h as $key=>$v) if($k===$v OR (is_array($v) && recursive_array_search($k,$v) !== false)) return $key;
 return false;
}

Usage:

echo recursive_array_search($searchThis, $InThisArray);

Now you just have to modify this to return whatever you want.

Source: php.net

answered Jun 27, 2014 at 7:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.