Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear

Stuck with a problem? Got Error? Ask AI support!

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 ); var_dump($distances); //get monimal distance position $index = array_keys( $distances, min($distances) )[0]; // print out closest number echo "Closest number is: " . $array[$index];
Copy Clear