0

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

echo $json[1]['ssl']

I would get Notice: Undefined index: 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?

AJFMEDIA
  • 2,093
  • 6
  • 29
  • 52
  • 1
    Possible duplicate of [How to convert two dimensional array to one dimensional array in php5](https://stackoverflow.com/questions/8754980/how-to-convert-two-dimensional-array-to-one-dimensional-array-in-php5) – Nigel Ren Jun 07 '18 at 11:43
  • Or check out https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array – Nigel Ren Jun 07 '18 at 11:44

2 Answers2

1

I think you might also use 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'];

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

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

Mr Glass
  • 1,186
  • 1
  • 6
  • 14