简体   繁体   English

添加/合并json数组

[英]Add to/merge json array

Im building a tool that measures websites for various things. 我正在构建一个工具来衡量网站的各种情况。

Im building up an array of info through each check. 我通过每次检查建立一系列信息。 Ill outline the logic below without loads of code. 病态概述了下面的逻辑,没有大量的代码。

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));

My result is this... 我的结果是

{
    "ssl": "true"
},
{
    "analytics": "true"
},
{
    "captchaResult": "CAPTCHA_NOT_NEEDED",
    "kind": "pagespeedonline#result",
    "responseCode": 200,

Now I try to read through it 现在我尝试通读

$report = file_get_contents("json.json");
$json = json_decode($report, true);

gives me.. 给我..

[0] => Array (
    [ssl] => true
    )
[1] => Array (
    [analytics] => true
    )
[3=> Array ( [captchaResult] => CAPTCHA_NOT_NEEDED
[kind] => pagespeedonline#result
[responseCode] => 200)

Unfortunately I cant determine in which order array 1 and 2 will be generated.. so if I try and echo a result like so 不幸的是,我无法确定数组1和2的生成顺序..因此,如果我尝试回显这样的结果

echo $json[1]['ssl']

I would get Notice: Undefined index: ssl. 我会得到通知:未定义的索引:ssl。

Ideally I would like to get the array like so: 理想情况下,我想像这样获取数组:

[0] => Array (
    [ssl] => true
    [analytics] => true
    [captchaResult] => CAPTCHA_NOT_NEEDED
    [kind] => pagespeedonline#result
    [responseCode] => 200
)

So I can simply echo out like so, regardless of the order: 因此,无论顺序如何,我都可以像这样简单地回显:

  echo $json['ssl'];
  echo $json['analytics'];
  echo $json['captureResult']; etc etc

How can I achieve this? 我该如何实现?

I think you might also use array_walk_recursive . 我认为您也可以使用array_walk_recursive

Because the result is a single array, you should make sure not to use duplicate values for the key. 因为结果是单个数组,所以您应确保不要对键使用重复的值。

$result = [];
array_walk_recursive($arrays, function ($value, $key) use (&$result) {
    $result[$key] = $value;
});

print_r($result);

Demo 演示

That would give you: 那会给你:

Array
(
    [ssl] => 1
    [analytics] => 1
    [captchaResult] => CAPTCHA_NOT_NEEDED
    [kind] => pagespeedonline#result
    [responseCode] => 200
)

You could get your value using for example echo $result['ssl']; 您可以使用echo $result['ssl'];获得您的价值echo $result['ssl'];

You could build the result yourself. 您可以自己构建结果。

// base array default values
// we use $defaults because we assume the remainder of this task 
// will be performed for multiple websites inside of a loop
$defaults = array('ssl'=>false, 'analytics'=>false, 'captchaResult'=>'CAPTCHA_NOT_NEEDED', 'kind'=>'', 'responseCode'=>0) ;

// get a base copy of your array
$results = $defaults ;

// loop through your results
foreach($json as $values) {
    // get your key 
    $key = key($values) ;

    // get your value
    $results[$key] = $values[$key] ;
}

That will get you a predictable array of values. 这将为您提供可预测的值数组。 Change your defaults to the correct values 将默认值更改为正确的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM