<?php
$json = '{
"shops": [
{
"shop_id": "100",
"locations": [
{ "location_id": "100_1", "distance": "10.3" },
{ "location_id": "100_2", "distance": "15.2" }
]
},
{
"shop_id": "101",
"locations": [
{ "location_id": "101_1", "distance": "19.3" },
{ "location_id": "101_2", "distance": "12.4" }
]
}]
}';
// Decode the JSON data
$json_data = json_decode($json,true);
// Do a foreach loop
foreach($json_data['shops'] as $shops) {
$distances = array_column($shops['locations'], 'distance','location_id');
asort($distances);
echo key($distances) . ' -> ' . current($distances) . "\n";
}
preferences:
106.32 ms | 408 KiB | 5 Q