3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Crate json string contain 50000 objecy $locations = ""; for ($i=0; $i<50000; $i++){ $locations .= "{\"location\":\"$i\",\"distance\":\"25.75206\"},"; } $json = "[".substr($locations, 0, -1)."]"; $location = '100'; // Using json_decode and loop $time = microtime(true); $locations = json_decode($json, true); foreach ($locations as $key => $value) { if ($value['location'] == $location) { @$distance = $value['distance']; } } echo "json_decode and loop:\n".(microtime(true) - $time)."\n"; // Using regex $time2 = microtime(true); if (preg_match("/\{\s*\"location\"\s*:\s*\"{$location}\".*?}/", $json, $match)){ $locations = json_decode("[{$match[0]}]", true)[0]; $distance = $locations['distance']; } echo "regex: \n".(microtime(true) - $time2);

preferences:
22.73 ms | 564 KiB | 5 Q