1

Totally a PHP noob here so sorry for the possibly basic question - here is what I'm trying achieve. I've got a dynamically generated array that looks something like:

$crumbs = 

(

[home] => Array ( 
[label] => Home 
[title] => Go to Home Page 
[first] => 1 
[last] => 
[readonly] => 
) 

[something] => Array ( 
[label] => Shop 
[title] => 
[first] => 
[last] => 1
[readonly] => 
) 

) 

I'm trying to figure out how I can write something to go through and search (at any depth) to see which array has the key "last" set to a value of 1. When the array with the key is found, I need to then echo out the "label" key for the array which had last = 1. Every array will always have a label key and only one array will ever have the "last" key set to 1. Thanks as always for the help!

1
  • You say at any depth but your example shows last and first always at the same depth. Do you need to search recursively or not? Commented Mar 27, 2012 at 8:09

3 Answers 3

2
$iterator = new RecursiveArrayIterator($crumbs);

foreach($iterator as $arr) {
  if($arr['last'] == 1) {
    echo $arr['label'];
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Have a look at the foreach construct

Comments

0

Use a foreach loop to go trough the array, or use the in_array function to see if the array contains a value of 1

Comments

Your Answer

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.