我有一个从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
)
)
)
希望,你是完全正确的。如果我将对象转换为数组,它将返回正确的数字!优秀的。
—
hellopat
您可能想接受答案。尤其是您在评论中写道的geiven,第一个给出的答案对您有效。
—
Andresch Serj
2009-08-21 11:05
,是与20个条目的数组,这样算($趋势)将理所当然地返回1