通过在php中找到它的值来计算json对象。

时间:2022-08-22 14:12:11

I have this json data . How can count the object using leader i want to count "leader": "true"

我有这个json数据。如何使用leader来计算对象,我要计算“leader”:“true”

this is my json: in this case the count of "leader":"true" is 1

这是我的json:在这种情况下,“leader”的计数:“true”是1。

[
{
"id":23,
"uid":"0090000219",
"cid":"0090000013",
"extension":"201",
"secret":"Myojyo42_f",
"leader":true,
"simultaneous":false,
"confbridge_id":17,
"created_at":"2015-09-02 12:49:12",
"updated_at":"2015-09-02 12:49:12"
},
{
"id":24,
"uid":"0090000221",
"cid":"0090000013",
"extension":"203",
"secret":"Myojyo42_f",
"leader":false,
"simultaneous":false,
"confbridge_id":17,
"created_at":"2015-09-02 12:49:12",
"updated_at":"2015-09-02 12:49:12"
},
{
"id":25,
"uid":"0090000223",
"cid":"0090000013",
"extension":"205",
"secret":"Myojyo42_f",
"leader":false,
"simultaneous":false,
"confbridge_id":17,
"created_at":"2015-09-10 10:16:24",
"updated_at":"2015-09-10 10:16:24"
}
]

This is my code:

这是我的代码:

        foreach($apiResults['conference_participants'] as &$record) {
           $leader_count = count(record['leader']);  
        }

3 个解决方案

#1


0  

Decode it first then it will return then count the data leader.

先解码它,然后它会返回,然后计数数据领袖。

Try this:

试试这个:

$decoded = json_decode($json, true);



     $count = 0;
    foreach($decoded as $key => $val)
    {
       if($val['leader'] == 1)
       {
           $count++;
       }
    }
    echo $count;
       //if you want also to count the false then add else in the statement echo  $count++;
    }

#2


1  

This will work... tested and confirmed

这将工作……测试和确认

$data = json_decode($json, true);
$count=0;
    foreach($data as $key => $val)
    {
       if($val['leader'] == 1)
       {
           $count++;
       }
    }
    echo $count;

#3


0  

This can be done by using the count function along with array_filter and json_decode.

这可以通过使用count函数以及array_filter和json_decode来实现。

echo count (array_filter(json_decode($json), 
                         function($a){ return $a->leader?1:0;}));

going from the inside out:

由内而外:

First json_decode makes the string into an array of stdClass objects

首先,json_decode将字符串变成stdClass对象的数组

Then array_filter uses the call back to filter out entries where the leader property is false.

然后array_filter使用回调来过滤leader属性为false的条目。

Finally count lets you know how many elements are in the returned array.

最后,count让您知道返回数组中有多少元素。

#1


0  

Decode it first then it will return then count the data leader.

先解码它,然后它会返回,然后计数数据领袖。

Try this:

试试这个:

$decoded = json_decode($json, true);



     $count = 0;
    foreach($decoded as $key => $val)
    {
       if($val['leader'] == 1)
       {
           $count++;
       }
    }
    echo $count;
       //if you want also to count the false then add else in the statement echo  $count++;
    }

#2


1  

This will work... tested and confirmed

这将工作……测试和确认

$data = json_decode($json, true);
$count=0;
    foreach($data as $key => $val)
    {
       if($val['leader'] == 1)
       {
           $count++;
       }
    }
    echo $count;

#3


0  

This can be done by using the count function along with array_filter and json_decode.

这可以通过使用count函数以及array_filter和json_decode来实现。

echo count (array_filter(json_decode($json), 
                         function($a){ return $a->leader?1:0;}));

going from the inside out:

由内而外:

First json_decode makes the string into an array of stdClass objects

首先,json_decode将字符串变成stdClass对象的数组

Then array_filter uses the call back to filter out entries where the leader property is false.

然后array_filter使用回调来过滤leader属性为false的条目。

Finally count lets you know how many elements are in the returned array.

最后,count让您知道返回数组中有多少元素。