php 如何在foreach循环中从匹配值中获取下一个值?[duplicate]

kognpnkq  于 2023-02-03  发布在  PHP
关注(0)|答案(3)|浏览(143)
    • 此问题在此处已有答案**:

Get next key in array(4个答案)
19小时前关门了。
如何在循环中从匹配值中获取下一个值?我需要数组中的"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); 
}
?>
q1qsirdb

q1qsirdb1#

试试这个

$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
kcugc4gi

kcugc4gi2#

<?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];
        ?>
7kqas0il

7kqas0il3#

请试试这个。

<?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/>;
}
?>

相关问题