Suggest closest matching 'correct' number from an array if, if statement returns false
<p>To simplify this question, I'm looking for php to suggest the nearest triangular number from an array to the number that has returned false.</p>

<p>For example if the value of the original number is 54 (which is not triangular), I'd like php to select the nearest two variables from an array that would fit this: 55 is and so is 45. Then echo them as suggestions. 
How can I achieve this?</p>

<p>To give some background information, here is my current code:</p>

<pre><code>&lt;?php 
$x = 54;
$area = $x * 8;
$ans1 = sqrt(1 + $area) -1;
$ans2 = sqrt(1 + $area) +1;

$answer1 = $ans1 / 2;
$answer2 = -$ans2 / 2;
$answer3 = $answer1 + $answer2;

    if ( is_numeric( $answer2 ) &amp;&amp; strpos( $answer2, '.' ) === false ){
        echo "$x is Triangular &lt;br /&gt; &lt;br /&gt;";
            if ($answer2 &gt; $answer1) {echo "Total number of glasses per side: ".$answer2."&lt;br /&gt;";}
            if ($answer2 &lt; $answer1) {echo "Total number of glasses per side: ".$answer1."&lt;br /&gt;";}
            }
    else {
        echo "$x is not Triangular &lt;br /&gt; &lt;br /&gt;";} 
?&gt;
</code></pre>
