0

I have the following JSON file

{
   "ResultDate":"mydate",
   "Tenant":"mytenant",
   "Results":[
      {
         "Config":[
            {
               "Check":null,
               "Object":null,
               "ConfigItem":"Default",
               "ConfigData":6,
               "InfoText":null,
               "Results":[
                  {
                     "Level":4,
                     "Value":null
                  },
                  {
                     "Level":5,
                     "Value":"Pass"
                  },
                  {
                     "Level":10,
                     "Value":"Fail"
                  },
                  {
                     "Level":15,
                     "Value":null
                  }
               ],
               "Level":5
            },
            

I now check if the file (JSON) exist and like to print the specific parts

//Check for every customer is the file exist
$filename = "../logs/" . $get_id_customer . "/myfile.json";

if (file_exists($filename)) {
    $message = "The file $filename exists";

    $filepath = $filename;
    $json_string = file_get_contents($filepath);
    $json = json_decode($json_string, true);

    //$json = json_decode($json_string, true);

    foreach($json['Results']['Config'] as $item) {
        $ConfigItem = $item['ConfigItem'];

        echo $ConfigItem;   
    }

But I don't get any results back. Have tried multiple different methods and examples but I dont get any result back from the JSON. Can anyone help me how to show the specific parts like configitem and configdata so I can workout the rest of my code?

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • 1
    "Results" is an array...so of course it doesn't have a "config" property. Access the first item of Results and then get the config property from that. Or loop through Results, if you're unsure how many items will be in it. And it's exactly the same for Config, which is also an array. Your current code should be producing a warning which you could use to narrow down the problem - if you don't see one, then turn on PHP's error/warning reporting – ADyson Feb 04 '22 at 13:27

2 Answers2

0

you can try

foreach($json['Results'][0]['Config'][0] as $item) {
    $ConfigItem = $item['ConfigItem'];
echo $ConfigItem;


}
Fabio Krueger
  • 65
  • 1
  • 8
  • you should explain _why_ your answer is good, not just dump the code. Explanatins are more useful and are more likely to get upvotes – ADyson Feb 04 '22 at 13:28
0

While I wouldn't recommend nested foreach loops generally - if you really want to you could do this...

<?php

//Check for every customer is the file exist
$filename = "../logs/" . $get_id_customer . "/myfile.json";

if (file_exists($filename)) {
    $message = "The file $filename exists";

    $filepath = $filename;
    $json_string = file_get_contents($filepath);
    $json = json_decode($json_string, true);

    //$json = json_decode($json_string, true);

    foreach ($json['Results'] as $result){
        foreach($result['Config'] as $item) {
            $ConfigItem = $item['ConfigItem'];
            echo $ConfigItem;
        }
    }
}

This is because Results is defined as an array which can imply there can be multiple items inside which we can loop over.

Simon K
  • 1,503
  • 1
  • 8
  • 10