在多维数组中获取具有相同索引的所有数组元素

时间:2013-10-24 08:08:13

标签: php oop multidimensional-array

请一个基本问题。有人可以帮助我获得所有三个主要类别字符串(要做的事情,交通和生活)。我只发现了如何单独使用它们

$categories[0]->maincategory;

但是如何才能获得所有这三个?非常感谢!

Array
(
[0] => stdClass Object
    (
        [id] => 2
        [maincategory] => Things to Do
        [category] => Activities and  Attractions
    )

[1] => stdClass Object
    (
        [id] => 6
        [maincategory] => Transportation
        [category] => Airport Transfers
    )

[2] => stdClass Object
    (
        [id] => 11
        [maincategory] => Living
        [category] => Broadband Internet
    )
)

2 个答案:

答案 0 :(得分:2)

类似

foreach ($stdClass as $sclass){
    if(!is_array($sclass)){
     echo $sclass.' ';
     }else{
    foreach ($sclass as $scl){


    echo $scl.' ';
    }
    }

    }

答案 1 :(得分:0)

简单的foreach循环可以解决问题。

foreach ($categories as $category) {
  echo $category->maincategory;
}