PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php // assign data to variables $array = [5,56,23,1,89,3]; $number = 90; // calculate distances between given number and each element of array $distances = array_map( function($el) use ($number) { return abs($el - $number); }, $array ); //get monimal distance position $index = array_keys( $distances, min($distances) )[0]; // print out closest number echo "Closest number is: " . $array[$index];
Show:  
Copy Clear