如何在PHP中查询结果中更新关联数组中的值

I have query results stored in a variable $result. I loop through $result and check for a particular key's value. If it is null, I want to have 'No' placed as value for the key 'customer'.

$result=$query->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row){
if (empty($row['customer'])) {
    $row['customer'] = 'No';
}

Now, I need to create a list from the $result.

$list=array();
foreach($result as $key => $value){
$list['result'][$key] = $value;
    var_dump($value);
}

But when I dump and see the values, I still see null instead of "No"

array
'name' => string 'Rachel' (length=6)
'customer' => null
array
'name' => string 'Kyndall' (length=7)
'customer' => null

Please let me know how I can have "No" as value for 'customer' key in the list if it is null. I need the list to export to csv file.

Thanks a lot.

change

foreach($result as $row){
if (empty($row['customer'])) {
    $row['customer'] = 'No';
}

to

foreach($result as $key => $row){
if (empty($row['customer'])) {
    $result[$key]['customer'] = 'No';
}

You need to iterate over a reference of $row to make it work

$result=$query->fetchAll(PDO::FETCH_ASSOC);
foreach($result as &$row){ // & = by reference
if (empty($row['customer'])) {
    $row['customer'] = 'No';
}

In your example each iteration will create a variable $row which has nothing to do with the contents mof $result. It's a copy.

Using & will instead create a reference to the original data, see http://php.net/manual/en/language.references.pass.php