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 }

preferences:
101.12 ms | 405 KiB | 5 Q