Mark Schrik Mark Schrik - 4 months ago 6x
JSON Question

Echo specific array items and use them in a if else statement

I am busy creating a on and off switch based on an item in an array.

The array is created from an API, and is returned in JSON.

$json = file_get_contents($url);

$data = json_decode($json,true);


The print_r result is this:

Array (
[GetDeviceListResult] => Array (
[Devices] => Array (
[0] => CanvasControllerDeviceCanvas Controller 1
[1] => LaCie 324 (2- Intel(R) Display
[2] => Realtek Digital Output (2- Real
[3] => SchedulerDevice
[4] => SneakerNetDevice
[5] => SystemDevice
[ServiceStatus] => Success


Let's say, i want the ServiceStatus. If its Success, make a green button, if its failure, make it a red button.

How do I accomplish this?


I edited your question and formatted your array so you can clearly see the structure. It becomes quite clear how to navigate it once you understand the structure!

So to answer you question:

$serviceStatus = $data['GetDeviceListResult']['ServiceStatus'];

And then of course:

if($serviceStatus == "Success") {
    // Display a green button!
} else {