-1

我有一个像这样的多维php数组

$array[0]   =   array('status' => 'Waiting' , 'number' => 10);
$array[1]   =   array('status' => 'Missed' , 'number' => 20);
$array[2]   =   array('status' => 'Waiting' , 'number' => 30);
$array[3]   =   array('status' => 'Missed' , 'number' => 40);
$array[4]   =   array('status' => 'Waiting' , 'number' => 50);
$array[5]   =   array('status' => 'Waiting' , 'number' => 60);
$array[6]   =   array('status' => 'Waiting' , 'number' => 70);

我想得到numbers,因为他们$array[4]之间没有。我怎么能用php做到这一点。什么是最简单的方法?$array[5]Missed

输出

50 and 60
4

3 回答 3

2
<?php
$array[0]   =   array('status' => 'Waiting' , 'number' => 10);
$array[1]   =   array('status' => 'Missed' , 'number' => 20);
$array[2]   =   array('status' => 'Waiting' , 'number' => 30);
$array[3]   =   array('status' => 'Missed' , 'number' => 40);
$array[4]   =   array('status' => 'Waiting' , 'number' => 50);
$array[5]   =   array('status' => 'Waiting' , 'number' => 60);
$array[6]   =   array('status' => 'Waiting' , 'number' => 70);
$last_status = '';
$last_number = '';
foreach($array as $foo){
    if($foo['status'] == $last_status && $last_status == 'Waiting'){
        echo "$last_number and $foo[number]";
        break;
    }
    $last_status = $foo['status'];
    $last_number = $foo['number'];
}
?>

键盘示例

于 2013-05-17T15:24:07.543 回答
0
for($i = 1; $i < count($array); $i++) {
    if($array[$i - 1]['status'] == 'Waiting' && $array[$i]['status'] == 'Waiting') {
        echo $array[$i - 1]['number'] . ' AND ' . $array[$i]['number'];
        break;
    }
}
于 2013-05-17T15:25:13.510 回答
0

另一种方法:

$array[0]   =   array('status' => 'Waiting' , 'number' => 10);
$array[1]   =   array('status' => 'Missed' , 'number' => 20);
$array[2]   =   array('status' => 'Waiting' , 'number' => 30);
$array[3]   =   array('status' => 'Missed' , 'number' => 40);
$array[4]   =   array('status' => 'Waiting' , 'number' => 50);
$array[5]   =   array('status' => 'Waiting' , 'number' => 60);
$array[6]   =   array('status' => 'Waiting' , 'number' => 70);

array_walk(
      $array
    , function ($value, $index, &$prev) {
        if ($value['status'] == $prev['status'] && $prev['status'] == 'Waiting') {
            echo $prev['number'] . ' AND ' . $value['number'] . PHP_EOL;
        }       

        $prev = $value; 

    }, array(
          'status' => ''
        , 'number' => 0
    )
);

查看演示

于 2013-05-17T16:14:51.777 回答