I am getting the below response with Azure rest API
JavaScript
x
33
33
1
{
2
"value": [
3
{
4
"id": "/subscriptions/xxxx-1faf-4756-a709-1af49be58e56/resourceGroups/cloud-shell-storage-centralindia",
5
"name": "cloud-shell-storage-centralindia",
6
"type": "Microsoft.Resources/resourceGroups",
7
"location": "centralindia",
8
"properties": {
9
"provisioningState": "Succeeded"
10
}
11
},
12
{
13
"id": "/subscriptions/xxxxx-1faf-4756-a709-1af49be58e56/resourceGroups/NetworkWatcherRG",
14
"name": "NetworkWatcherRG",
15
"type": "Microsoft.Resources/resourceGroups",
16
"location": "eastus",
17
"properties": {
18
"provisioningState": "Succeeded"
19
}
20
},
21
{
22
"id": "/subscriptions/xxxx-1faf-4756-a709-1af49be58e56/resourceGroups/AZREUSADRG",
23
"name": "AZREUSADRG",
24
"type": "Microsoft.Resources/resourceGroups",
25
"location": "eastus",
26
"properties": {
27
"provisioningState": "Succeeded"
28
}
29
},
30
31
]
32
}
33
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.
JavaScript
1
57
57
1
$response = curl_exec($curl);
2
//echo $response;
3
$json=json_decode($response,true);
4
//$value=$data[0];
5
//echo $value;
6
//print_r($datarg);
7
8
// Define function
9
function print_recursive($arr){
10
11
foreach ($arr as $key => $val) {
12
if (is_array($val)) {
13
print_recursive($val);
14
15
} else {
16
echo("$key = $val <br/>");
17
}
18
}
19
return;
20
}
21
22
// Call function
23
//print_recursive($json);
24
25
// Recursive function to search by key
26
function search_recursive_by_key($arr, $searchkey){
27
$items = array();
28
foreach ($arr as $key => $val) {
29
if (is_array($val)) {
30
search_recursive_by_key($val, $searchkey);
31
32
} else
33
34
{
35
if ($searchkey == $key) {
36
37
38
echo("$val <br/>");
39
40
41
42
}
43
44
}
45
//print_r("$val <br/>");
46
47
}
48
49
50
return;
51
52
}
53
54
// Call function with Key as second argument
55
$arraynew=search_recursive_by_key($json, 'name');
56
?>
57
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.
Advertisement
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“:
JavaScript
1
38
38
1
<?php
2
3
$j = '{
4
"value": [
5
{
6
"id": "/subscriptions/xxxx-1faf-4756-a709-1af49be58e56/resourceGroups/cloud-shell-storage-centralindia",
7
"name": "cloud-shell-storage-centralindia",
8
"type": "Microsoft.Resources/resourceGroups",
9
"location": "centralindia",
10
"properties": {
11
"provisioningState": "Succeeded"
12
}
13
},
14
{
15
"id": "/subscriptions/xxxxx-1faf-4756-a709-1af49be58e56/resourceGroups/NetworkWatcherRG",
16
"name": "NetworkWatcherRG",
17
"type": "Microsoft.Resources/resourceGroups",
18
"location": "eastus",
19
"properties": {
20
"provisioningState": "Succeeded"
21
}
22
},
23
{
24
"id": "/subscriptions/xxxx-1faf-4756-a709-1af49be58e56/resourceGroups/AZREUSADRG",
25
"name": "AZREUSADRG",
26
"type": "Microsoft.Resources/resourceGroups",
27
"location": "eastus",
28
"properties": {
29
"provisioningState": "Succeeded"
30
}
31
}
32
]
33
}';
34
35
$arr = json_decode($j, true);
36
$names = array_column($arr['value'], 'name');
37
print_r($names);
38
will output:
JavaScript
1
7
1
Array
2
(
3
[0] => cloud-shell-storage-centralindia
4
[1] => NetworkWatcherRG
5
[2] => AZREUSADRG
6
)
7