动态获取数组的名称

时间:2013-05-09 12:55:30

标签: php arrays

我有一个看起来像这样的数组:

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

现在我想用foreach进行迭代以获取0,4,2!

的内容

通常我会写:

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

但这显然不起作用,因为数组名称总是不同的......在这种情况下我怎么能迭代?

1 个答案:

答案 0 :(得分:1)

只需添加第二个循环来迭代成员:

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