3v4l.org

run code in 300+ PHP versions simultaneously
<?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"; }
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
100_1 -> 10.3 101_2 -> 12.4

preferences:
145.38 ms | 407 KiB | 5 Q