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?

12
  • 1
    stackoverflow.com/questions/29308898/… Commented Jun 7, 2016 at 14:43
  • 1
    $data['GetDeviceListResult']['ServiceStatus'] is how you access the service status. Commented Jun 7, 2016 at 14:44
  • seriously, am i that retarded. Well, thank you very much! Commented Jun 7, 2016 at 14:45
  • 1
    @Midas or just view-source in your browser, which is what I do. ;-) Commented Jun 7, 2016 at 14:50
  • 1
    @MarkSchrik $data['GetDeviceListResult']['Devices'][3]. No single quote around the number 3. Commented Jun 7, 2016 at 14:50

2 Answers 2

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 {
    //
}
Sign up to request clarification or add additional context in comments.

Comments

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>";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.