【发布时间】:2018-06-07 11:33:31
【问题描述】:
我正在构建一个工具来衡量网站的各种情况。
我正在通过每次检查建立一系列信息。我将在没有大量代码的情况下概述下面的逻辑。
var report = [];
//do a check for an ssl cert, if true...
report.push({
"ssl": "true"
});
//do a check for analytics tag, if true...
report.push({
"analytics": "true"
});
//Then I run the google insights api and add results to the array...
report.push(JSON.parse(data));
我的结果是这样的……
{
"ssl": "true"
},
{
"analytics": "true"
},
{
"captchaResult": "CAPTCHA_NOT_NEEDED",
"kind": "pagespeedonline#result",
"responseCode": 200,
现在我尝试通读它
$report = file_get_contents("json.json");
$json = json_decode($report, true);
给我..
[0] => Array (
[ssl] => true
)
[1] => Array (
[analytics] => true
)
[3=> Array ( [captchaResult] => CAPTCHA_NOT_NEEDED
[kind] => pagespeedonline#result
[responseCode] => 200)
不幸的是,我无法确定数组 1 和 2 的生成顺序。所以如果我尝试回显这样的结果
echo $json[1]['ssl']
我会收到通知:未定义索引:ssl。
理想情况下,我希望得到这样的数组:
[0] => Array (
[ssl] => true
[analytics] => true
[captchaResult] => CAPTCHA_NOT_NEEDED
[kind] => pagespeedonline#result
[responseCode] => 200
)
所以无论顺序如何,我都可以简单地回显:
echo $json['ssl'];
echo $json['analytics'];
echo $json['captureResult']; etc etc
我怎样才能做到这一点?
【问题讨论】:
标签: php arrays json array-merge array-push