开发者

In PHP, how can I tell what values are adjacent to a specific value in an array?

开发者 https://www.devze.com 2023-03-13 10:08 出处:网络
I have an array of values.Let\'s say the array is like this: [apple, banana, coconut, duku, emblica, fig, gooseberry]

I have an array of values. Let's say the array is like this:

[apple, banana, coconut, duku, emblica, fig, gooseberry]

Let's say that I know a specific value, "fig." How can I know which values are next to it, both before a开发者_运维百科nd after?


$index = array_search("fig", $array);
$before = "";
$after = "";
if($index === false){
    echo "Not found";
}else{
    $before = $index > 0 ? $array[$index - 1] : "";
    $after = ($index + 1) < count($array) ? $array[$index + 1] : "";
}


Assuming the keys are sequential:

$key = array_search($fruit, 'fig');
if ($key === FALSE) {
    echo 'No figs in array';
} else {
    echo "Before: ", $fruit[$key-1];
    echo "After: ", $fruit[$key+1];
}


You would use

$key = array_search($array); 
$leftVal = $array[$key - 1];
$rightVal = $array[$key + 1];

The array_search() function returns the index of the value in the array, and then just increment/de-increment to find adjacent values.


The accepted answer is almost correct, but it does not handle "missing" elements so well.

You can use the function array_key_exists() to validate whether a key exists, and this also works as a bounds check.

Try this:

<?php

function array_before_after($stext,$array) {
$index = array_search($stext, $array);
$before = "";
$after = "";
    if($index === false){
        echo "Not found";
    }else{
        $before = array_key_exists($index - 1,$array) ? $array[$index - 1] : "";
        $after = array_key_exists($index + 1,$array) ? $array[$index + 1] : "";
    }
    return array($before,$after);
}

$my_array = array( 1 => "apple", 2 => "banana", 3 => "coconut", 6 => "fig", 7 => "gooseberry");

$my_stext = "fig";
$a1 = array_before_after($my_stext, $my_array);
echo "'$a1[0]', '$my_stext', '$a1[1]'\n";

$my_stext = "apple";
$a2 = array_before_after($my_stext, $my_array);
echo "'$a2[0]', '$my_stext', '$a2[1]'\n";

$my_stext = "gooseberry";
$a3 = array_before_after($my_stext, $my_array);
echo "'$a3[0]', '$my_stext', '$a3[1]'\n";

?>

If you want the preceding value, you can use reset() and next():

<?php

function array_before_after($stext,$array) {
    $my_array = $array;
    $val = reset($my_array);
    $before = "";
    $after="";
    $lim = count($my_array);
    for ($i=1; $i<$lim; $i++) {
        if ($val == $stext) {
            if ( $i<$lim ) $after=next($my_array);
            break;
        } else {
            $before = $val;
        }
        $val = next($my_array);
    }
    return array($before,$after);
}

$my_array = array( 1 => "apple", 2 => "banana", 3 => "coconut", 6 => "fig", 7 => "gooseberry");

$my_stext = "fig";
$a1 = array_before_after($my_stext, $my_array);
echo "'$a1[0]', '$my_stext', '$a1[1]'\n";

$my_stext = "apple";
$a2 = array_before_after($my_stext, $my_array);
echo "'$a2[0]', '$my_stext', '$a2[1]'\n";

$my_stext = "gooseberry";
$a3 = array_before_after($my_stext, $my_array);
echo "'$a3[0]', '$my_stext', '$a3[1]'\n";

?>


$my_array = ['apple', 'banana', 'coconut', 'duku', 'emblica', 'fig', 'gooseberry'];

echo $my_array[ 5 ]; // Will print fig
echo $my_array[ 4 ]; // Will print emblica
echo $my_array[ 6 ]; // Will print gooseberry
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号