3v4l.org

run code in 300+ PHP versions simultaneously
<?php $prices = array( array('id' => '28','price' =>100 ), array('id' => '29','price' =>105 ), array('id' => '30','price' =>110 ), array('id' => '31','price' =>115 ), array('id' => '32','price' =>120 ), array('id' => '33','price' =>125 ), array('id' => '34','price' =>130 ) ); function getNearestHighest($prices, $amount){ $resullt = null; $diff = PHP_INT_MAX; foreach($prices as $p){ if($p['price'] >= $amount){ if(abs($p['price'] - $amount) < $diff){ $result = $p; $diff = abs($p['price'] - $amount); } } } if(is_null($result)) throw new \Exception("No nearest highest amount exists!"); return $result; } try{ print_r(getNearestHighest($prices, 113)); }catch(\Exception $e){ // whatever you wish to do }
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
Array ( [id] => 31 [price] => 115 )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
122.18 ms | 407 KiB | 5 Q