php 查找 json 数组中的对象数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15872554/
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
Find the number of objects in a json array
提问by user2201395
I have searched a lot and I found few methods to find the length of my JSON array. I have tried:
我搜索了很多,但发现很少有方法可以找到我的 JSON 数组的长度。我试过了:
countjson.length
countjson.length
but these return 1, instead of the actual length. I want to show it using PHP.
但这些返回 1,而不是实际长度。我想用 PHP 来展示它。
My json is:
我的 json 是:
$huge = '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"},{"location":[{"building":["test_loc1_building1","test_loc1_building2"],"name":"test location1"},{"building":["test_loc2_building2"],"name":"test location2"}],"name":"test Organization"}]';
How can I find the number of objects in my JSON array?
如何在我的 JSON 数组中找到对象的数量?
回答by alwaysLearn
You need to decode the jsonobject and then count the elements in it ..
您需要解码json对象,然后计算其中的元素..
$json_array = json_decode($json_string, true);
$elementCount = count($json_array);
回答by Stegrex
You'll need to decode into PHP arrays before you do any work with the data.
在对数据进行任何操作之前,您需要解码为 PHP 数组。
Try:
尝试:
$hugeArray = json_decode($huge, true); // Where variable $huge holds your JSON string.
echo count($hugeArray);
If you need to count to a lower depth, you'll need to iterate through the array.
如果您需要计数到较低的深度,则需要遍历数组。
For example, if you want to count the number of elements in the next layer, you can do:
例如,如果你想统计下一层的元素数量,你可以这样做:
foreach ($hugeArray as $key => $value) {
echo $key.' - '.count($value);
}
However, this isn't necessarily meaningful because it depends on what you are trying to count, what your goal is. This block only counts the number of elements 1 layer underneath, regardless of what the actual numbers could mean.
但是,这不一定有意义,因为这取决于您要计算什么,您的目标是什么。该块只计算下面 1 层的元素数量,而不管实际数字可能意味着什么。
回答by Yogesh Suthar
First decode your jsonand after that use counton it.
首先解码你json,然后使用count它。
$huge='[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"},{"location":[{"building":["test_loc1_building1","test_loc1_building2"],"name":"test location1"},{"building":["test_loc2_building2"],"name":"test location2"}],"name":"test Organization"}]';
$arr = json_decode($huge,true);
echo count($arr);
回答by sectus
Object (an unordered collection of key:value pairs with the ':' character separating the key and the value, comma-separated and enclosed in curly braces; ...)
对象(键:值对的无序集合,用“:”字符分隔键和值,逗号分隔并用花括号括起来;...)
So, all you have to do are just count opened curly braces.
所以,你所要做的就是计算打开的花括号。
substr_count($huge , '{');
But... If you are storing some strings with '{' in json you can't do it. In that way you have to write your own simple parser or regular expression.
但是...如果你在 json 中存储一些带有 '{' 的字符串,你就不能这样做。通过这种方式,您必须编写自己的简单解析器或正则表达式。
But... the easies way to json_decode. And use recursive function if you want to get count of allobjects in json.
但是... json_decode 的简单方法。如果您想获取json中所有对象的数量,请使用递归函数。
function count_objects($value)
{
if (is_scalar($value) || is_null($value))
$count = 0;
elseif (is_array($value))
{
$count = 0;
foreach ($value as $val)
$count += count_objects($val);
}
elseif (is_object($value))
{
$count = 1;
$reflection_object = new \ReflectionObject($value);
foreach ($reflection_object->getProperties() as $property)
{
$count +=count_objects($property->getValue($value));
}
}
return $count;
}
$huge = '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"},{"location":[{"building":["test_loc1_building1","test_loc1_building2"],"name":"test location1"},{"building":["test_loc2_building2"],"name":"test location2"}],"name":"test Organization"}]';
echo count_objects(json_decode($huge));
回答by Lee ChengRi
We can use ".sizeof()" function. So
我们可以使用“.sizeof()”函数。所以

