2011-03-29 160 views
1

我還是PHP新手,似乎無法計算另一個對象內的對象數量。 stdClass對象如下所示:PHP:計數另一個對象內的對象數量?

stdClass Object (

[data] => Array (
    [0] => stdClass Object (
     [Code] => ABC 
     [Title] => Alphabet 
     [sections] => Array (
      [0] => stdClass Object (
       [Name] => Sounds 
       [sections] => Vowels 
      ) 
     ) 
    ) 

) 

我必須計算此對象中元素的數量,以便我可以正確回顯它。對於數據,我能夠做到這一點:

$number = count($hanap->data); 

我不知道該怎麼做的部分。

$number = count($hanap->data->sections); // does not work. 

謝謝。任何幫助將不勝感激。 :)

+0

$ total = count((array)$ obj); http://stackoverflow.com/questions/1314745/php-count-an-stdclass-object – yousef 2017-04-05 10:26:17

回答

1
count($hanap->data[0]->sections) 
+0

非常抱歉的麻煩。我很粗心。 :P謝謝! – Joshua 2011-03-29 08:23:46

1

你缺少,他們是數組的第一個成員......

$number = count($hanap->data[0]->sections) 
+0

對不起,我很抱歉。我很粗心。 :P謝謝! – Joshua 2011-03-29 08:24:10

相關問題