PHP:计算另一个对象中的对象数量?
我还是 PHP 新手,我似乎无法计算另一个对象中对象的数量。 stdClass 对象如下所示:
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.
谢谢。任何帮助将不胜感激。 :)
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 it 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. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将解决您的问题,只需将对象转换为数组并对其进行计数
PHP:计算 stdClass 对象< /a>
this will solve your problem, just cast the object to array and count it
PHP: Count an stdClass object
您缺少数组中的第一个成员......
You are missing the first member of the array where they are...