gpt4 book ai didi

javascript - 使用数组内容进行 Json 解码

转载 作者:行者123 更新时间:2023-12-03 04:19:50 24 4
gpt4 key购买 nike

我正在训练自己(JSON 和 PHP),但遇到了问题我的 Json 解码如下所示:

"playerstats": {
"steamID": "75068112",
"gameName": "ValveTestApp260",
"stats": [
{
"name": "total_kills",
"value": 2314497
},
{
"name": "total_deaths",
"value": 1811387
},

并解析为 php 我这样做:

$url = 'myrul';
$content = file_get_contents($url);
$json = json_decode($content, true);
echo $json['playerstats']['stats'][1]['name']

它可以工作,但问题是当我更改 id 来获取统计信息时,数组的顺序不一样:/所以我不能使用 [2] 或任何其他数字来获取数据。

我在这里发帖是为了了解如何使用每个数组的属性名称(例如 'total_kills')而不是 [1]..[2].. 获取统计信息

感谢大家的支持

最佳答案

使用foreach并使用if条件来检查名称'total_kills'是否为true,然后将其存储到新数组中,如下所示。

    <?php
$json = json_decode($content, true);
$new_array =array();
$i=0;
foreach($json['playerstats']['stats'] as $row )
{

if($row['name']=='total_kills')
{

$new_array[]=array($row['name']=>$row['value']);
}

if($i<10)
{

break;
}
$i++
}

print_r($new_array);
?>

关于javascript - 使用数组内容进行 Json 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43997939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com