[英]In PHP, how can I tell what values are adjacent to a specific value in an array?
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."假设我知道一个特定的值,“fig”。 How can I know which values are next to it, both before and 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. array_search() function 返回数组中值的索引,然后只需递增/递减以查找相邻值。
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.您可以使用 function array_key_exists() 来验证键是否存在,这也可以作为边界检查。
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() :如果你想要前面的值,你可以使用reset()和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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.