1

I am busy creating a on and off switch based on an item in an array. I am not looking to extract the data from JSON, that already happened.

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

$json = file_get_contents($url);

$data = json_decode($json,true);

print_r($data);

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 
    ) 
)

Signs4u

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?

2 Answers2

1

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 {
    //
}
jszobody
  • 28,495
  • 6
  • 61
  • 72
0

A more generic solution would be to create a recursive function that searches for a given key in a multidimensional array, for example, if this function searches for the key "ServiceStatus", it would return "Success", or, if it searches for the key "4", it returns "SneakerNetDevice", here is the function :

<?php
// FUNCTION THAT SEARCHES A KEY IN A MULTIDIMENSIONAL ARRAY.
// RETURNS VALUE FOUND, OR EMPTY STRING IF NOT FOUND.
function search_key ( $arr,$search_key )
{ $value = ""; // EMPTY IF NOTHING IS FOUND.
  $keys = array_keys( $arr ); // GET ALL KEYS FROM CURRENT ARRAY.
  foreach ( $keys as $key ) // LOOP THROUGH ALL KEYS IN ARRAY.
    if ( is_array( $arr[ $key ] ) ) // IF CURRENT ITEM IS SUB-ARRAY...
       if ( array_key_exists( $search_key,$arr[ $key ] ) ) // IF ARRAY CONTAINS KEY
         { $value = $arr[ $key ][ $search_key ]; // VALUE FOUND.
           break;
         }
    else $value = search_key( $arr[ $key ],$search_key ); // ENTER SUB-ARRAY.
  return $value;
}

// EXAMPLE ARRAY.
$arr = 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"
            ) 
        );

// HOW TO USE FUNCTION.
if ( search_key( $arr,"ServiceStatus" ) == "Success" )
     echo "<button>Green button</button>";
else echo "<button>Red button</button>";
echo "<br/>";
if ( search_key( $arr,"4" ) == "SneakerNetDevice" )
     echo "<button>Blue button</button>";
else echo "<button>Yellow button</button>";
?>