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

$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);
}


我怎样才能做到这一点?
谢谢

最佳答案

您可以使用如下的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'] );
    }
}

关于php - CodeIgniter-从数组中获取下一条记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23889666/

10-14 14:41