PHP:计算一个stdClass对象 [英] PHP: Count a stdClass object

查看:74
本文介绍了PHP:计算一个stdClass对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a stdClass object created from json_decode that won't return the right number when I run the count($obj) function. The object has 30 properties, but the return on the count() function is say 1.

有什么想法吗?

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

Below is an example of one of the objects. (I'm requesting the daily trend information from Twitter). If this object had more than one property, the count($obj) would equal 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
                    )

            )
     )

推荐答案

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

The problem is that count is intended to count the indexes in an array, not the properties on an object, (unless it's a custom object that implements the Countable interface). Try casting the object, like below, as an array and seeing if that helps.

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

仅将对象强制转换为数组并不总是可行,但是作为一个简单的stdClass对象,它应该可以在此处完成工作.

Simply casting an object as an array won't always work but being a simple stdClass object it should get the job done here.

这篇关于PHP:计算一个stdClass对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆