douzhan1031 2015-02-18 16:50
浏览 30
已采纳

无法将两个数组合并为一个来获取sql查询

Its might sounding like a noob question but I can't find an answer. Currently I have this.

$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $gridData[] = array(
                'CompanyName' => $row['CompanyName'],
                'ContactName' => $row['ContactName'],
                'ContactTitle' => $row['ContactTitle'],
            'Address' => $row['Address'],
            'Country' => $row['Country'],
            'Parent' => $row['Parent'],
            'City' => $row['City'],
            'id' => $row['id']
      );
}

but assuming I don't want to hardcode this fields there I have another array which is stored in $fields and has the structure of an array inside so field [0] is the fieldname.

so I do

 foreach ($fields as $field)
     {
       $gridData[$field[0]] => $row[$field[0]];
     }

by some reason it does not work like this. what is the way to do this properly? How can I bring this together?

  • 写回答

3条回答 默认 最新

  • duanchi6377 2015-02-18 17:07
    关注
    $gridData[] = array() 
    

    will add a numerical index to the $gridData array with value of what is in the array. You are trying to do

    $gridData[$field[0]] => $row[$field[0]];
    

    which is trying to reference a string index. Try:

    foreach ($gridData as $rowIndex => $gridRow){
      foreach ($fields as $field)
      {
        $gridData[$rowIndex][$field[0]] => $row[$field[0]];
      }
    }
    

    Alternatively, if you would be ok modifying your field data structure, you could use http://php.net/manual/en/function.array-merge.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题