如何从foreach循环中的匹配值获取下一个值
How to get next value from match value in foreach loop
如何从循环中的匹配值中获取下一个值。数组中需要“1010-1040”键。
<?php
$arr = Array
(
'0900-0930' => 0,
'0935-1005' => 0,
'1010-1040' => 0,
'1045-1115' => 0
);
$matchVal = '0935-1005';
foreach ($arr as $key => $value) {
if($matchVal == $key){
echo $key.'<br />';
}
echo next($arr);
}
?>
提前致谢。
<?php
$arr = Array
(
'0900-0930' => 0,
'0935-1005' => 0,
'1010-1040' => 0,
'1045-1115' => 0
);
$keys = array_keys($arr);
$matchVal = '0935-1005';
$matched = array_search($matchVal, $keys); // $key = 1;
$nextindex = $matched + 1;
$nextValue = $arr[$nextindex];
?>
试试这个
$arr = Array
(
'0900-0930' => 0,
'0935-1005' => 0,
'1010-1040' => 0,
'1045-1115' => 0
);
$matchVal = '0935-1005';
$keys = array_keys($arr);
print $keys[array_search($matchVal,$keys)+1];
输出
1010-1040
请试试这个。
<?php
$arr = Array
(
'0900-0930' => 0,
'0935-1005' => 0,
'1010-1040' => 0,
'1045-1115' => 0
);
$matchVal = '0935-1005';
foreach ($arr as $key => $value) {
if($matchVal == $key){
echo $key.'<br />';
}
next($arr);
echo key($arr)."<br/>;
}
?>
如何从循环中的匹配值中获取下一个值。数组中需要“1010-1040”键。
<?php
$arr = Array
(
'0900-0930' => 0,
'0935-1005' => 0,
'1010-1040' => 0,
'1045-1115' => 0
);
$matchVal = '0935-1005';
foreach ($arr as $key => $value) {
if($matchVal == $key){
echo $key.'<br />';
}
echo next($arr);
}
?>
提前致谢。
<?php
$arr = Array
(
'0900-0930' => 0,
'0935-1005' => 0,
'1010-1040' => 0,
'1045-1115' => 0
);
$keys = array_keys($arr);
$matchVal = '0935-1005';
$matched = array_search($matchVal, $keys); // $key = 1;
$nextindex = $matched + 1;
$nextValue = $arr[$nextindex];
?>
试试这个
$arr = Array
(
'0900-0930' => 0,
'0935-1005' => 0,
'1010-1040' => 0,
'1045-1115' => 0
);
$matchVal = '0935-1005';
$keys = array_keys($arr);
print $keys[array_search($matchVal,$keys)+1];
输出
1010-1040
请试试这个。
<?php
$arr = Array
(
'0900-0930' => 0,
'0935-1005' => 0,
'1010-1040' => 0,
'1045-1115' => 0
);
$matchVal = '0935-1005';
foreach ($arr as $key => $value) {
if($matchVal == $key){
echo $key.'<br />';
}
next($arr);
echo key($arr)."<br/>;
}
?>