首页 > 解决方案 > 如何在 PHP 中处理 Azure REST API 响应

问题描述

我使用 Azure REST API 得到以下响应

{
"value": [
        {
            "id": "/subscriptions/xxxx-1faf-4756-a709-1af49be58e56/resourceGroups/cloud-shell-storage-centralindia",
            "name": "cloud-shell-storage-centralindia",
            "type": "Microsoft.Resources/resourceGroups",
            "location": "centralindia",
            "properties": {
                "provisioningState": "Succeeded"
            }
        },
        {
            "id": "/subscriptions/xxxxx-1faf-4756-a709-1af49be58e56/resourceGroups/NetworkWatcherRG",
            "name": "NetworkWatcherRG",
            "type": "Microsoft.Resources/resourceGroups",
            "location": "eastus",
            "properties": {
                "provisioningState": "Succeeded"
            }
        },
        {
            "id": "/subscriptions/xxxx-1faf-4756-a709-1af49be58e56/resourceGroups/AZREUSADRG",
            "name": "AZREUSADRG",
            "type": "Microsoft.Resources/resourceGroups",
            "location": "eastus",
            "properties": {
                "provisioningState": "Succeeded"
            }
        },
        
    ]
} 

我想在 PHP 的 Array 中添加键 Name 的值,我该怎么做。我已经尝试过这段代码,但我只能打印。

$response = curl_exec($curl);
//echo $response;
$json=json_decode($response,true);
//$value=$data[0];
//echo $value;
//print_r($datarg);

// Define function
function print_recursive($arr){

    foreach ($arr as $key => $val) {
      if (is_array($val)) {
        print_recursive($val);
  
      } else {
         echo("$key = $val <br/>");
      }
    }
  return;
  }
  
  // Call function
  //print_recursive($json);

// Recursive function to search by key
function search_recursive_by_key($arr, $searchkey){
  $items = array();
    foreach ($arr as $key => $val) {
       if (is_array($val)) {
          search_recursive_by_key($val, $searchkey);
 
       } else 
       
       {
          if ($searchkey == $key) {
          
               
            echo("$val <br/>");
           
            
            
          }
          
        }
        //print_r("$val <br/>"); 
      
      }  
      
      
      return;
 
}

// Call function with Key as second argument
$arraynew=search_recursive_by_key($json, 'name');
?>

一旦我能够在数组中添加名称,我将使用该数组来填充我的应用程序中的下拉列表。

任何 PHP 专家都可以在这里提供帮助。

标签: javascriptphparraysjsonazure

解决方案


一旦你得到有效的 JSON(我,从你的 JSON 中删除了最后一个,所以它是有效的)你可以使用array_​column从输入数组中的单个列返回值”:

<?php

$j = '{
"value": [
        {
            "id": "/subscriptions/xxxx-1faf-4756-a709-1af49be58e56/resourceGroups/cloud-shell-storage-centralindia",
            "name": "cloud-shell-storage-centralindia",
            "type": "Microsoft.Resources/resourceGroups",
            "location": "centralindia",
            "properties": {
                "provisioningState": "Succeeded"
            }
        },
        {
            "id": "/subscriptions/xxxxx-1faf-4756-a709-1af49be58e56/resourceGroups/NetworkWatcherRG",
            "name": "NetworkWatcherRG",
            "type": "Microsoft.Resources/resourceGroups",
            "location": "eastus",
            "properties": {
                "provisioningState": "Succeeded"
            }
        },
        {
            "id": "/subscriptions/xxxx-1faf-4756-a709-1af49be58e56/resourceGroups/AZREUSADRG",
            "name": "AZREUSADRG",
            "type": "Microsoft.Resources/resourceGroups",
            "location": "eastus",
            "properties": {
                "provisioningState": "Succeeded"
            }
        }
    ]
}';

$arr = json_decode($j, true);
$names = array_column($arr['value'], 'name');
print_r($names);

将输出:

Array
(
    [0] => cloud-shell-storage-centralindia
    [1] => NetworkWatcherRG
    [2] => AZREUSADRG
)

推荐阅读