Find position and get before and after?
<pre><code>$x = "P-29042011386693";
$array = "P-28042011135870,P-28042011132333,P-28042011384621,P-29042011386693,P-29042011384190,P-29042011388434,P-29042011382454,P-29042011385512,P-29042011383902";
$array = explode(",", $array);
$count = count($array);
$search = array_search($x, $array);

if (($search &gt; 0) &amp;&amp; ($search &lt; $count)){
    $before = $array[$search-1];
    $after = $array[$search+1];
}elseif ($search == 0){
    $before = NULL;
    $after = $array[1];
}elseif ($search == $count){
    $before = $array[$count-1];
    $after = NULL;
}
</code></pre>

<p>What is the best way to detect the value before $x and the value after $x, and display $before or $after as blank if there is no result before/after $x?</p>

<p>So if $x was P-29042011383902 - $after would be blank and $before would be P-29042011385512</p>

<p>If $x was P-28042011135870 $before would be blank and $after would be P-28042011132333</p>

<p>Is my code above right?</p>
