为什么在PHP中为数组分配新值会失败?

时间:2014-05-27 14:42:19

标签: php

考虑:

array (size=1)
  0 =>
    array (size=5)
      'name' => string '15268459735 Farming and Projects XXXXX' (length=38)
      'region' => string '2' (length=1)
      'entitynumber' => string '2012/002086/24' (length=14)
      'ownership' => string '6' (length=1)
      'id' => string '26249' (length=5)

为什么以下内容仍然返回没有owner元素的相同数组?

foreach ($result as $row)
{
    $row['owner'] = 1;
}

1 个答案:

答案 0 :(得分:5)

尝试passing the array by reference

foreach ($result as &$row) {
    $row['owner'] = 1;
}

另请参阅:What's the & for, anyway?