1
array(1) {
  ["farm"]=>
  array(2) {
    ["folder1"]=>
    array(2) {
      ["horse"]=>
      array(1) {
        ["fred.jpg"]=>
        string(30) "farm/folder1/horse/fred.jpg"
      }
      ["cat"]=>
      array(1) {
        ["john.jpg"]=>
        string(28) "farm/folder1/cat/john.jpg"
      }
    }
    ["folder2"]=>
    array(1) {
      ["cat"]=>
      array(2) {
        ["sam.jpg"]=>
        string(27) "farm/folder2/cat/sam.jpg"
        ["cat"]=>
        array(1) {
          ["john.jpg"]=>
          string(32) "farm/folder2/cat/cat/john.jpg"
        }
      }
    }
  }
}

Is it possible to detect only the most inner array keys?

foreach($array as $key => $value){
   if ($key == $most_inner_array) {
     echo $key;
   }
}

So that in this case the result would be:

fred.jpg
john.jpg
sam.jpg
john.jpg
6
  • 1
    I always wonder why would you guys want to do such things. Commented Apr 13, 2016 at 13:34
  • @PhiterFernandes Why is this unusual for you? Commented Apr 13, 2016 at 13:36
  • I don't know, I just don't picture a scenario where this would be necessary. I'm just a very curious person. Commented Apr 13, 2016 at 13:37
  • 1
    @PhiterFernandes this looks like a file system, you want to catch every files, not folders Commented Apr 13, 2016 at 13:40
  • 1
    @Random Yes exactly! Commented Apr 13, 2016 at 13:41

1 Answer 1

1

You can go through your array recursively and print the key if the value is not an array.

In case of just printing a value like in your example, you can do it with just array_walk_recursive() without any condition. Something like:

array_walk_recursive($your_array, function($value, $key) {
    echo $key . "\n";
});
Sign up to request clarification or add additional context in comments.

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.