Php 统计变量(如果存在)

Php 统计变量(如果存在),php,Php,下面是我的原始json文件的一小部分,如果用户在游戏中,则游戏字段显示在json中,如果没有在游戏中,则json将删除游戏字段。我试图通过计算游戏区域来计算游戏中有多少用户 原始json 我所做的一部分是 $count = count($result['members']); 我像这样重复着 <strong><?php echo $count; ?></strong> 但是没有结果使用 $countingame=count($result['members

下面是我的原始json文件的一小部分,如果用户在游戏中,则游戏字段显示在json中,如果没有在游戏中,则json将删除游戏字段。我试图通过计算游戏区域来计算游戏中有多少用户

原始json

我所做的一部分是

$count = count($result['members']);
我像这样重复着

<strong><?php echo $count; ?></strong>
但是没有结果

使用

$countingame=count($result['members'][0]['game])

As成员是一个数组

使用

$countingame=count($result['members'][0]['game])


因为成员是一个数组

我已经测试过了,应该可以工作了:

JSON代码:

{
    "members": [
        {
            "username": "Squadman",
            "game": {
                "name": "World of Warcraft"
            }
        },
        {
            "username": "Bob",
            "game": {
                "name": "Battlefield"
            }
        },
        {
            "username": "Jane"
        }
    ]
}
PHP代码:

<?php

$json = file_get_contents('https://example.com/file.json');

$decodedJson = json_decode($json, true);
$array = $decodedJson['members'];

$count = 0;
foreach ($array as $key => $value) {
    if(isset($value['game']['name']) && $value['game']['name'] != '') {
        $count++;
    }
}

echo $count;

?>

我已经测试过了,应该可以:

JSON代码:

{
    "members": [
        {
            "username": "Squadman",
            "game": {
                "name": "World of Warcraft"
            }
        },
        {
            "username": "Bob",
            "game": {
                "name": "Battlefield"
            }
        },
        {
            "username": "Jane"
        }
    ]
}
PHP代码:

<?php

$json = file_get_contents('https://example.com/file.json');

$decodedJson = json_decode($json, true);
$array = $decodedJson['members'];

$count = 0;
foreach ($array as $key => $value) {
    if(isset($value['game']['name']) && $value['game']['name'] != '') {
        $count++;
    }
}

echo $count;

?>

要计算游戏中的成员数,您需要检查从json_decode获得的成员数组中是否存在“game”键。你可以用isset来做

$result = json_decode($result, true);

$count = count($result['members']);
for ($countingame = $i = 0; $i++; $i < $count)
    $countingame += isset($result['members'][$i]['game']) ? 1 : 0;

echo "$count members in $countingame games";
$result=json\u decode($result,true);
$count=count($result['members']);
对于($countingame=$i=0;$i++;$i<$count)
$countingame+=isset($result['members'][$i]['game'])?1 : 0;
echo“$countingame游戏中的$count成员”;

要计算游戏中的成员数,您需要检查从json_decode获得的成员数组中是否存在“game”键。你可以用isset来做

$result = json_decode($result, true);

$count = count($result['members']);
for ($countingame = $i = 0; $i++; $i < $count)
    $countingame += isset($result['members'][$i]['game']) ? 1 : 0;

echo "$count members in $countingame games";
$result=json\u decode($result,true);
$count=count($result['members']);
对于($countingame=$i=0;$i++;$i<$count)
$countingame+=isset($result['members'][$i]['game'])?1 : 0;
echo“$countingame游戏中的$count成员”;

它给了我两个错误注意:未定义索引:游戏进入和警告:count():参数必须是数组或实现可数的对象首先将json转换为数组/对象。示例:$result=json_decode($json,true);对于arrayit,它给了我两个错误注意:未定义索引:game in和Warning:count():参数必须是数组或实现Countable的对象首先将json转换为数组/对象。示例:$result=json_decode($json,true);对于arrayare,您是否使用$assoc true或false或省略调用json_decode?您是否使用$assoc true或false或省略调用json_decode?这在某种程度上起到了作用。。如果用户在游戏中,它会显示,但对于不在游戏中的用户,它会显示未定义的错误。如我在问题中所述,如果用户是ingame,json会添加场上游戏,但如果没有json会删除游戏,因此对于那些不在游戏中的用户,它会使用
if(isset($value['game']['name'])和&$value['game']['name']!='
@JaiprakashKhowal从一开始就打算添加它,但我想,“嗯,这样就可以了。”哈哈,谢谢。我编辑了原始答案。我们可以不使用if(!empty($value['game']['name']){$count++;}@MichaelEugeneYuen是的,有很多方法可以检查一个是否不存在。你可以使用
!isset
null
is_null
empty
,等等…我会让OP来决定。这是一种有效的方法。如果用户在游戏中,它会显示,但对于不在游戏中的用户,它会显示,并且错误是未定义的在我的问题中,如果用户是ingame,json会添加场上游戏,但如果没有json会删除游戏,因此对于那些不在游戏中的用户,它会使用
if(isset($value['game']['name'])和&$value['game']['name']!=”
@jaiprakashkhhowal从一开始就打算添加,但他想,“meh,这就可以了。哈哈,谢谢。我编辑了原始答案。我们可以不使用if(!empty($value['game']['name']){$count++;}@MichaelEugeneYuen是的,有很多方法可以检查一个是否不存在。你可以使用
!isset
null
is_null
empty
,等等。我将由OP来决定。