<?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');
$locations = array_column($shops['locations'], 'location_id');
$min = min($distances);
$location = array_search($min, $distances);
echo $locations[$location] . ' -> ' . $min . "\n";
}
preferences:
24.28 ms | 407 KiB | 5 Q