2013-10-24 225 views
0

請注意一個基本問題。有人可以幫助我獲得所有三個maincategory字符串(事情做,交通和生活)。我只發現如何讓他們個人獲取多維數組中所有具有相同索引的數組元素

$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 
    ) 
) 
+0

那麼我們應該得到你的初始數據結構?和期望的結果? –

回答

2

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


    echo $scl.' '; 
    } 
    } 

    } 
0

簡單foreach循環會做的伎倆。

foreach ($categories as $category) { 
    echo $category->maincategory; 
} 
相關問題