PHP:计算另一个对象中的对象数量?

发布于 2024-10-27 11:30:46 字数 641 浏览 3 评论 0原文

我还是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

琴流音 2024-11-03 11:30:46

这将解决您的问题,只需将对象转换为数组并对其进行计数

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

PHP:计算 stdClass 对象< /a>

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

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

PHP: Count an stdClass object

彼岸花ソ最美的依靠 2024-11-03 11:30:46
count($hanap->data[0]->sections)
count($hanap->data[0]->sections)
终难愈 2024-11-03 11:30:46

您缺少数组中的第一个成员......

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

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

$number = count($hanap->data[0]->sections)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文