dongxianshuai8927 2013-03-07 02:39
浏览 51
已采纳

将SQL PHP数组添加到现有数组中

I have two database tables. One contains Marketplaces and the other contains the Locations of the Marketplaces. Marketplaces can have multiple locations the two tables are joined by the MarketplaceID. I need to query the marketplaces turn it into an array and during the foreach or the while, add the locations as an array to the marketplaces. This is all part of two json responses. However my goal is to combine the data together and have one json response where the locations are within the marketplaces json data as "Locations". Ultimately an array where Marketplaces is the parent and Locations are the children.

Code 1 (Table1 Query):

$sql = "SELECT * FROM Marketplaces WHERE Status = 1";
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)){   
  foreach($row as $key => $value){
      $arr[$key] = $value;
  }
  $main_arr[] = $arr;
}
return $main_arr;

Result:

[{"MarketplaceID":"1","AccountID":"0","BusinessName":"Mike's Pub","BusinessType":"1","Status":"1","CreationDate":"0000-00-00 00:00:00"}]

Code 2 (Table2 Query):

$sql = "SELECT * FROM Locations WHERE MarketplaceID = 1";
$results = mysql_query($sql);

while($row = mysql_fetch_assoc($results)){
  foreach($row as $key => $value){
    $arr[$key]=$value;
  }
  $main_arr[] = $arr;
}

Result:

[{"LocationID":"1","MarketplaceID":"1","Address1":"5055 Business Center Drive","Address2":"Suite 100","City":"San Francisco","CA":"1","Zip":"90210","Phone1":"(555) 555-555","Phone2":"(555) 555-5555","EmailAddress":"email@msn.com","StoreNumber":"200"},{"LocationID":"2","MarketplaceID":"1","Address1":"112 Street Court","Address2":"","City":"Los Angeles","StateID":"2","Zip":"90210","Phone1":"(555) 555-6666","Phone2":"","EmailAddress":"email@gmail.com","StoreNumber":"300"}]
  • 写回答

2条回答 默认 最新

  • dongwu1410 2013-03-07 03:01
    关注

    First, drop this

      foreach($row as $key => $value){
          $arr[$key] = $value;
      }
    

    It does nothing except the same as $arr = $row; Actually it does something else - produces a warning as $arr is undefined.

    EDIT Updated answer now that the initial query has been changed from WHERE MarketplaceID=1 to WHERE Status = 1"

    $main_arr=array();
    $sql = "SELECT * FROM Marketplaces WHERE Status = 1";
    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result)){   
      $row['locations']=array();
      $sql2 = "SELECT * FROM Locations WHERE MarketplaceID = ".$row['MarketplaceID'];
      $results2 = mysql_query($sql2);
      while($row2 = mysql_fetch_assoc($results2)){
          $row['locations'][]=$row2;
      }
      $main_arr[] = $row; 
    }
    
    // print_r($main_arr); and view page source to read, rather than a JSON string
    

    Actually I would do this slightly differently so that my array doesn't muddle locations inside of Marketplace but still keeps them together, like this below

    while($row = mysql_fetch_assoc($result)){   
      $thisrow=array('marketplace'=>$row, 'locations'=>array());
      $sql2 = "SELECT * FROM Locations WHERE MarketplaceID = ".$row['MarketplaceID'];
      $results2 = mysql_query($sql2);
      while($row2 = mysql_fetch_assoc($results2)){
          $thisrow['locations'][]=$row2;
      }
      $main_arr[] = $thisrow; 
    }
    

    so the object would be

    { 
        "marketplace": {"MarketplaceID": 1, "BusinessName": "Fred Bloggs", ... }
        "locations": [
            {"LocationId": 1, "Address1": "Some address"},
            {"LocationId": 2, "Address1": "Some address"}
        ]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加