0

First posting and looked/searched for 2 days and find items similar but cannot find a nested example so hope its correctly formatted as a question.

I have the test json from a file below:

{
  "name": "Cars and Trucks",
  "cars": [
    {
      "cartestvar": "Car Test Var 1",
      "carheader": "Car Test Header 1",
      "cartypes": [
        [
          "Ford",
          "BMW",
          "Fiat"
        ],
        [
          "Dodge",
          "Lincoln",
          "Corvette"
        ]
      ]
    }
  ]
}

And I am looking to echo out the cartypes values. I understand for loops but foreach gets me. I tried:

//$json = file_get_contents('test.json');
$json = file_get_contents('test.json');
//var_dump($json);
$json_a = json_decode($json, true);
$value1 = $json_a['cars'][0].['cartypes'][0][0];
echo '<br/>'. $value1 .'<br/>';

But that returns nothing. The $value1 test is just the first step to even see 1 value before walking recursively through the json.

For additional reference, the current for loop:

if (json_last_error() === JSON_ERROR_NONE) { 
    //do something with $json. It's ready to use 
    echo 'got here <br/>';
    for($x = 0; $x < 2; $x++) {
        for($y = 0; $y < 3; $y++){  
            //$value1 = $json_a['cartypes'][0][0];
            //echo '<br/>'. $value1 .'<br/>';
        }
    }
} else { 
    //yep, it's not JSON. Log error or alert someone or do nothing 
}

0 Answers0