PHP:对 stdClass 对象进行计数

2022-08-30 06:54:40

我有一个从json_decode创建的stdClass对象,当我运行count($obj)函数时,它不会返回正确的数字。该对象有 30 个属性,但 count() 函数的返回值显示为 1。

有什么想法吗?

下面是其中一个对象的示例。(我从Twitter请求每日趋势信息)。如果此对象具有多个属性,则 count($obj) 将等于 1。

[trends] => stdClass Object
    (
        [2009-08-21 11:05] => Array
            (
                [0] => stdClass Object
                    (
                        [query] => "Follow Friday"
                        [name] => Follow Friday
                    )

                [1] => stdClass Object
                    (
                        [query] => "Inglourious Basterds" OR "Inglorious Basterds"
                        [name] => Inglourious Basterds
                    )

                [2] => stdClass Object
                    (
                        [query] => Inglourious
                        [name] => Inglourious
                    )

                [3] => stdClass Object
                    (
                        [query] => #songsincode
                        [name] => #songsincode
                    )

                [4] => stdClass Object
                    (
                        [query] => #shoutout
                        [name] => #shoutout
                    )

                [5] => stdClass Object
                    (
                        [query] => "District 9"
                        [name] => District 9
                    )

                [6] => stdClass Object
                    (
                        [query] => #howmanypeople
                        [name] => #howmanypeople
                    )

                [7] => stdClass Object
                    (
                        [query] => Ashes OR #ashes
                        [name] => Ashes
                    )

                [8] => stdClass Object
                    (
                        [query] => #youtubefail
                        [name] => #youtubefail
                    )

                [9] => stdClass Object
                    (
                        [query] => TGIF
                        [name] => TGIF
                    )

                [10] => stdClass Object
                    (
                        [query] => #wish09
                        [name] => #wish09
                    )

                [11] => stdClass Object
                    (
                        [query] => #watch
                        [name] => #watch
                    )

                [12] => stdClass Object
                    (
                        [query] => Avatar
                        [name] => Avatar
                    )

                [13] => stdClass Object
                    (
                        [query] => Ramadhan
                        [name] => Ramadhan
                    )

                [14] => stdClass Object
                    (
                        [query] => Goodnight
                        [name] => Goodnight
                    )

                [15] => stdClass Object
                    (
                        [query] => iPhone
                        [name] => iPhone
                    )

                [16] => stdClass Object
                    (
                        [query] => #iranelection
                        [name] => #iranelection
                    )

                [17] => stdClass Object
                    (
                        [query] => Apple
                        [name] => Apple
                    )

                [18] => stdClass Object
                    (
                        [query] => "Usain Bolt"
                        [name] => Usain Bolt
                    )

                [19] => stdClass Object
                    (
                        [query] => H1N1
                        [name] => H1N1
                    )

            )
     )

答案 1

问题在于 count 旨在对数组中的索引进行计数,而不是对对象上的属性进行计数(除非它是实现 Countable 接口的自定义对象)。尝试将对象(如下所示)转换为数组,看看是否有帮助。

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

简单地将对象转换为数组并不总是有效,但是作为一个简单的stdClass对象,它应该在这里完成工作。


答案 2

计数函数旨在用于

  1. 阵 列
  2. 从实现可数接口的类派生的对象

stdClass 既不是这些。完成你所追求的更简单/最快捷的方法是

$count = count(get_object_vars($some_std_class_object));

这使用PHP的get_object_vars函数,该函数将以数组的形式返回对象的属性。然后,您可以将此数组与 PHP 的 count 函数一起使用。


推荐