回顯特定的數組項,並在if else語句中使用它們

[英]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. I am not looking to extract the data from JSON, that already happened.

我正忙着根據數組中的項創建一個on和off開關。我不打算從已經發生的JSON中提取數據。

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

該數組是從API創建的,並以JSON格式返回。

$json = file_get_contents($url);

$data = json_decode($json,true);

print_r($data);

The print_r result is this:

print_r結果如下:

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.

比方說,我想要ServiceStatus。如果成功,請按綠色按鈕,如果失敗,請將其設為紅色按鈕。

How do I accomplish this?

我該如何做到這一點?

2 个解决方案

#1


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 {
    //
}

#2


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 :

更通用的解決方案是創建一個遞歸函數,在多維數組中搜索給定鍵,例如,如果此函數搜索鍵“ServiceStatus”,它將返回“Success”,或者,如果它搜索鍵“4”,它返回“SneakerNetDevice”,這里是功能:

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/06/07/7df53bc5862dcac2d9945d31eecb0f78.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com