【问题标题】:PHP: Count number of objects within another object?PHP:计算另一个对象中的对象数量?
【发布时间】:2011-07-25 02:02:02
【问题描述】:

我还是 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.

谢谢。任何帮助将不胜感激。 :)

【问题讨论】:

标签: php object stdclass


【解决方案1】:

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

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

PHP: Count an stdClass object

【讨论】:

    【解决方案2】:
    count($hanap->data[0]->sections)
    

    【讨论】:

      【解决方案3】:

      您缺少它们所在的数组的第一个成员...

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

      【讨论】:

        猜你喜欢
        • 2013-05-07
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        • 2014-10-08
        • 2020-03-10
        • 2018-10-06
        • 2011-04-15
        • 2020-04-05
        相关资源
        最近更新 更多