Php 查找json数组中的对象数
我搜索了很多,找到了一些方法来查找JSON数组的长度。我试过:Php 查找json数组中的对象数,php,json,Php,Json,我搜索了很多,找到了一些方法来查找JSON数组的长度。我试过: 计数 json.length 但它们返回1,而不是实际长度。我想用PHP来展示它 我的json是: $huge = '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"},{"location":[{"building":["test_loc1_building1","test
计数
json.length
$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"}]';
如何在我的JSON数组中找到对象的数量?在处理数据之前,需要先解码到PHP数组中 尝试: 如果需要计数到较低的深度,则需要遍历数组 例如,如果要计算下一层中的元素数,可以执行以下操作:
foreach ($hugeArray as $key => $value) {
echo $key.' - '.count($value);
}
然而,这并不一定有意义,因为这取决于你想计算什么,你的目标是什么。此块仅统计下一层的元素数,而不考虑实际数字的含义。首先解码您的
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);
您需要创建一个对象,然后计算其中的元素
$json_array = json_decode($json_string, true);
$elementCount = count($json_array);
对象(键:值对的无序集合,用“:”字符分隔键和值,逗号分隔并用大括号括起来;…)
所以,你所要做的就是数一数打开的花括号
substr_count($huge , '{');
但是。。。如果你在json中存储一些带有“{”的字符串,你就不能这样做。这样你就必须编写自己的简单解析器或正则表达式
但是…json解码的简单方法。如果您想获得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));
我们可以使用“.sizeof()”函数。
因此JSON是一种序列化格式,如何对其进行反序列化和检查?这里有另一个不使用JSON_decode的解决方案。这里有另一个不使用JSON_decode的解决方案:现在如何打印每个小文件中的对象数array@user2201395然后遍历数组的元素并回显count()如果您不需要$json_数组作为PHP变量,您可以通过嵌套
$elementCount=count(json_decode($json_string,true));
将其缩短为一行。但这行不通,因为它包含方括号和引号。
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));