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 git.master, git.master_jit, rfc.property-hooks
Array ( [id] => 31 [price] => 115 )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
32.55 ms | 405 KiB | 5 Q