CodeIgniter - 从数组中获取下一条记录

时间:2014-05-27 12:39:19

标签: php mysql arrays codeigniter

我从表中获取所有数据到数组:

$records = $this->Misc->getAll('table');

接下来我做了一个循环

foreach ($records as $key => $record) {     
    // here i want to update a column in the fist
    // row with a value from the second row
    $this->Misc->update($record->id; $value_of_a_column_from_the_next_row);
}

我该怎么做? 感谢

1 个答案:

答案 0 :(得分:-1)

你可以使用你的foreach循环:

foreach ($records as $key => $record) {
    if (isset($records[$key-1]) {
        $updateID = $records[$key-1];
        $this->Misc->update($updateID, $record['field'] );
    }
}

或者相反:

foreach ($records as $key => $record) {
    if (isset($records[$key+1]) {
        $updateID = $records[$key+1];
        $this->Misc->update($updateID, $record['field'] );
    }
}