How to process the Azure REST API response in PHP

Tags: , , , ,



I am getting the below response with 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"
            }
        },
        
    ]
} 

I want to add the value of the key Name in Array in PHP how can I do that. I have tried this code but I can only able to print.

$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');
?>

Once I am able to add the names in an array I will use that array to populate the drop down list in my application.

Can any PHP expert help here.

Answer

Once you get valid JSON (I removed the last , from your JSON so it is valid) you can use array_​column to “return the values from a single column in the input array“:

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

will output:

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


Source: stackoverflow