PHP:计算 stdClass 对象的数量

发布于 2024-08-02 18:46:08 字数 4037 浏览 8 评论 0原文

我有一个从 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
                    )

            )
     )

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.

Any ideas?

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
                    )

            )
     )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

一萌ing 2024-08-09 18:46:08

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

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

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

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);

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

宛菡 2024-08-09 18:46:08

count 函数旨在用于从

  1. 实现可计数接口的类派生的数组对象,

stdClass 则不是其中之一。完成您所追求的任务的最简单/最快捷的方法是

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

使用 PHP 的 get_object_vars 函数,它将返回以下属性作为数组的对象。然后您可以将这个数组与 PHP 的 count 函数一起使用。

The count function is meant to be used on

  1. Arrays
  2. Objects that are derived from classes that implement the countable interface

A stdClass is neither of these. The easier/quickest way to accomplish what you're after is

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

This uses PHP's get_object_vars function, which will return the properties of an object as an array. You can then use this array with PHP's count function.

↘紸啶 2024-08-09 18:46:08

该对象没有 30 个属性。它有一个,它是一个包含 30 个元素的数组。您需要该数组中的元素数量。

The object doesn't have 30 properties. It has one, which is an array that has 30 elements. You need the number of elements in that array.

谁的年少不轻狂 2024-08-09 18:46:08

这里的 count() 没有任何问题,“趋势”是在这种情况下被计算的唯一键,你可以尝试这样做:

count($obj->trends);

或者:

count($obj->trends['2009-08-21 11:05']);

或者甚至可以这样做:

count($obj, COUNT_RECURSIVE);

There is nothing wrong with count() here, "trends" is the only key that is being counted in this case, you can try doing:

count($obj->trends);

Or:

count($obj->trends['2009-08-21 11:05']);

Or maybe even doing:

count($obj, COUNT_RECURSIVE);
姐不稀罕 2024-08-09 18:46:08

您可以使用 ArrayIterator 来实现此目的。

(new \ArrayIterator($obj))->count()

You can use ArrayIterator for that purpose.

(new \ArrayIterator($obj))->count()
百合的盛世恋 2024-08-09 18:46:08

只需使用这个

$i=0;
foreach ($object as $key =>$value)
{
$i++;
}

变量 $i 就是键的数量。

Just use this

$i=0;
foreach ($object as $key =>$value)
{
$i++;
}

the variable $i is number of keys.

迷雾森÷林ヴ 2024-08-09 18:46:08

您可以使用 sizeof() 函数来获取 PHP 中任何对象的长度!

you can use sizeof() function to get the length of any object in PHP!

停滞 2024-08-09 18:46:08

计数普通数组或对象

count($object_or_array); 

计数多维数组或对象

count($object_or_array, 1); // 1 for  multidimensional array count, 0 for Default

Count Normal arrya or object

count($object_or_array); 

Count multidimensional arrya or object

count($object_or_array, 1); // 1 for  multidimensional array count, 0 for Default
浅笑依然 2024-08-09 18:46:08

count() 函数适用于数组。但如果你想计算物体的长度,那么你可以使用这个方法。

$total = $obj->length;

count() function works with array. But if you want to count object's length then you can use this method.

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