0

The API is returning the following json:

    {
    "car1": [
        {
            "car_name": "audi",
            "gear_box_type": "Automatic",
            "num_of_seats": "2",
            "num_of_doors": "3",
            "imagePath": "/images/a3.png"
        }
    ]
  } 

The following is my php code:

<select class="form-control" id="exampleFormControlSelect1">
<?php

$obj = new ApiHelper();
$result = $obj->CallAPI("GET","http://localhost:5000/cars", "NONE");
$jsondata = json_decode($result, true);
$car = $jsondata['car1'];
echo "<option>".$car."</option>";
?>
</select>

The output will be "Array". Image of select

I would like to know how I can access this array that is inside the json.

Any help much appreciated thanks.

Elliott Weeks
  • 83
  • 1
  • 5

1 Answers1

0

The array is accessed like any standard PHP array. You can Google search "PHP arrays" to find more information. One helpful function is print_r($array_name).

So:

print_r($jsondata);

Will give you a printout of the structure of the array, making it easy for you to then determine what is available to you in the array. If you view the output of this function in your browser, be sure to right-click on the page and select "View Source." Otherwise the structure will be printed out as one long string that isn't very fun to read.

Hope this helps!