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

时间:2013-03-07 02:39:06

标签: php sql json foreach while-loop

我有两个数据库表。一个包含Marketplaces,另一个包含Marketplaces的位置。市场可以有多个位置,两个表由MarketplaceID连接。我需要查询市场将其转换为数组,并在foreach或while期间,将位置作为数组添加到市场中。这是两个json响应的一部分。然而,我的目标是将数据组合在一起并且具有一个json响应,其中位置在市场json数据中作为“位置”。最终是一个阵列,其中Marketplaces是父母,Locations是孩子。

代码1(表1查询):

$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;

结果:

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

代码2(表2查询):

$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;
}

结果:

[{"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 个答案:

答案 0 :(得分:0)

首先,放弃

  foreach($row as $key => $value){
      $arr[$key] = $value;
  }
除了$arr = $row之外,它什么都不做;实际上它会做其他事情 - 产生警告,因为$arr未定义。

编辑现在,如果初始查询已从WHERE MarketplaceID=1更改为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

实际上我会略微区别这样做,以便我的阵列不会混淆市场内的位置,但仍然将它们保持在一起,如下所示

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; 
}

所以对象将是

{ 
    "marketplace": {"MarketplaceID": 1, "BusinessName": "Fred Bloggs", ... }
    "locations": [
        {"LocationId": 1, "Address1": "Some address"},
        {"LocationId": 2, "Address1": "Some address"}
    ]
}

答案 1 :(得分:0)

如果我正确读取它,你的第一个数组是一个包含Marketplace 1数据的单维数组,而你的第二个数组是一个多维数组,其中父数组中的每个元素都包含一个子数组,其中包含有关a的信息。单一地点。如果要将第二个数组放在第一个数组中,请尝试嵌套循环,如下所示:

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

while($row = mysql_fetch_assoc($result)){   

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

}

$sql2 = "SELECT * FROM Locations WHERE MarketplaceID = 1";
$results2 = mysql_query($sql2);

while($row2 = mysql_fetch_assoc($results2)){

  foreach($row2 as $key => $value){
  $arr2[$key]=$value;

}

$arr['locations'] = $arr2;

$main_arr = $arr;

}

这将创建一个临时数组$ arr2,用于存放您的位置数据,然后将其存储在$ arr ['locations']中。在代码的末尾,您要添加到$ main_arr的$ arr应该具有以下元素:“MarketplaceID”,“AccountID”,“BusinessName”,“BusinessType”,“Status”,“CreationDate”,“location” “和”location“应该是一个包含元素的数组,这些数组包含从数据库中提取的位置信息。

希望这能回答你的问题。