foreach数组和更新

时间:2014-06-28 19:50:40

标签: php arrays

在循环foreach时,我正在努力更新数组中的值。我得到的只是最后一张唱片。

$books = array(
  (int) 0 => array(
    'Page' => array(
        'id' => '30',
        'order' => '0'
    )
),
  (int) 1 => array(
    'Page' => array(
        'id' => '4',
        'order' => '1'
    )
),
  (int) 2 => array(
    'Page' => array(
        'id' => '7',
        'order' => '2'
    )
),
  (int) 3 => array(
    'Page' => array(
        'id' => '36',
        'order' => '4'
    )
)
)

我正在尝试更新['Page'] ['order']从0开始并递增1。

$i = 0;
foreach ($books as $book) {
    $book['Page']['order'] = $i;
    $i++;
};

1 个答案:

答案 0 :(得分:1)

$i = 0;
foreach ($books as $book) {
    $books[$book]['Page']['order'] = $i;
    $i++;
};