Jurudocs
Jurudocs

Reputation: 9165

Getting the name of an array dynamically

I have an array which looks like this:

$example = [
             ['rendered'][0]['rendereditem1']
             ['rendered'][4]['rendereditem2 and more']
             ['rendered'][2]['rendereditem3']
]

Now I want to iterate with foreach to get the contents of 0,4,2!

Normally I would write:

foreach($example as $value){
    print $value['rendered'][int which is the same everywhere];
}

But that is obviously not working because the array name is always different...how could I iterate in this case?

Upvotes: 1

Views: 49

Answers (1)

Nick Andriopoulos
Nick Andriopoulos

Reputation: 10643

Simply add a second loop to iterate over the members :

foreach($example as $value) {
  foreach($value['rendered'] as $key=>$item) {
    // Do what you want here, $key is 0,4,2 in your example
  }
}

Upvotes: 1

Related Questions