PHP:计算一个 stdClass 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1314745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Count a stdClass object
提问by hellopat
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.
我有一个从 json_decode 创建的 stdClass 对象,当我运行 count($obj) 函数时它不会返回正确的数字。该对象有 30 个属性,但 count() 函数的返回值是 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.
下面是其中一个对象的示例。(我从 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
)
)
)
回答by Steven Surowiec
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.
问题是 count 旨在计算数组中的索引,而不是对象上的属性(除非它是实现 Countable 接口的自定义对象)。尝试将对象(如下所示)转换为数组,看看是否有帮助。
$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.
简单地将对象转换为数组并不总是有效,但作为一个简单的 stdClass 对象,它应该在这里完成工作。
回答by Alan Storm
The count function is meant to be used on
计数功能旨在用于
- Arrays
- 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
stdClass 不是这两者。完成您所追求的事情的更简单/最快捷的方法是
$count = count(get_object_vars($some_std_class_object));
This uses PHP's get_object_varsfunction, which will return the properties of an object as an array. You can then use this array with PHP's count function.
这使用 PHP 的get_object_vars函数,它将以数组的形式返回对象的属性。然后,您可以将此数组与 PHP 的 count 函数一起使用。
回答by Rob Drimmie
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.
该对象没有 30 个属性。它有一个,它是一个有 30 个元素的数组。您需要该数组中的元素数。
回答by Alix Axel
There is nothing wrong with count() here, "trends" is the only key that is being counted in this case, you can try doing:
count() 在这里没有任何问题,“趋势”是这种情况下唯一计算的键,您可以尝试执行以下操作:
count($obj->trends);
Or:
或者:
count($obj->trends['2009-08-21 11:05']);
Or maybe even doing:
或者甚至可以这样做:
count($obj, COUNT_RECURSIVE);
回答by Arash Younesi
Just use this
就用这个
$i=0;
foreach ($object as $key =>$value)
{
$i++;
}
the variable $iis number of keys.
变量$i是键的数量。
回答by Himal Majumder
Count Normal arrya or object
计数正常的 arrya 或对象
count($object_or_array);
Count multidimensional arrya or object
计算多维 arrya 或对象
count($object_or_array, 1); // 1 for multidimensional array count, 0 for Default
回答by WaQaR Ali
count()function works with array. But if you want to count object's length then you can use this method.
count()函数适用于数组。但是如果你想计算对象的长度,那么你可以使用这个方法。
$total = $obj->length;

