doumao9363 2013-04-29 14:16
浏览 15
已采纳

在PHP循环中更改关键等于某事的关联数组值

I have a situation where I am looping through a result set from the DB in MYSQL:

$result = mysql_query("SELECT * FROM orders ORDER BY repID") or die('Query failed!');
while(false !== ($row = mysql_fetch_assoc($result))) {

    if(!$flag) {
       // display field/column names as first row
       fputcsv($out, array_keys($row), ',', '"');
       $flag = true;
    }

    array_walk($row, 'cleanData');

    fputcsv($out, array_values($row), ',', '"');
}

So this prints out the array keys as column headers for the first row.

In some rows I have ID's that come directly out of the table, so I need to replace the values in specific rows with a function to use the given ID as a search parameter in a DB query.

I just cant seem to find a way to change the array value of a specific column within the loop... any ideas?

  • 写回答

1条回答 默认 最新

  • dongre6404 2013-04-29 15:25
    关注

    You can use the ampersand modifier with the foreach loop as follows.

    $nameArray = array('test1', 'test2', 'test1');    
    
    foreach ($nameArray as &$value) 
        {
            if ($value === "test1") 
            {
                $value = 'test2';
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?