开发者

PHP: Count number of objects within another object?

I'm still new at PHP and I can't seem to count the number of Objects within another object. The stdClass object looks like this:

stdClass Object (

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

)

I must count the number of elements in this object so i can echo i开发者_如何学Got properly. For the data, I was able to do it:

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

I don't know how to do it for the sections.

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

Thanks. Any help will be greatly appreciated. :)


this will solve your problem, just cast the object to array and count it

$total = count((array)$obj);

PHP: Count an stdClass object


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


You are missing the first member of the array where they are...

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜