Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在关联数组中联接,而不是在分离的记录中联接_Php_Mysql - Fatal编程技术网

Php 在关联数组中联接,而不是在分离的记录中联接

Php 在关联数组中联接,而不是在分离的记录中联接,php,mysql,Php,Mysql,表存储 id name date 1 foo 2011-06-15 15:10:34 2 bar 2011-07-02 16:45:18 表位置 storeid zipcode latitude longitude 1 90001 xxxxx xxxxx 1 45802 xxxxx xxxxx 2 32843 xxxxx xxxxx 如何生成包含名为locations的键的关联数组,

存储

id  name  date
1   foo   2011-06-15 15:10:34
2   bar   2011-07-02 16:45:18
位置

storeid  zipcode  latitude  longitude
1        90001    xxxxx     xxxxx
1        45802    xxxxx     xxxxx
2        32843    xxxxx     xxxxx
如何生成包含名为
locations
的键的关联数组,该键是存储区所有位置的数组

我当前的SQL(将记录中的每个位置分隔开):

我想要的示例:

array(
 [0] => array(
           "id" => 1,
           "name" => "foo",
           "locations" => array(
                           [0] => array(
                                    "zipcode" => 90001,
                                    "latitude" => -45.48513,
                                    "longitude" => 82.12432
                                   )
                           [1] => array(
                                    "zipcode" => 42802,
                                    "latitude" => -31.48513,
                                    "longitude" => 77.12432
                                   )
                          ) 
         )
)
其他商店也是如此


谢谢

所以您不能在一个查询中提取数据,因为SQL通常每行工作,并且没有像PHP数组那样的数据结构。无法使用JOIN嵌套记录。这就是为什么必须在PHP循环中使用单独的查询。像这样:

$query = "SELECT s.id,s.name FROM stores AS s";

$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_assoc( $result )) {
    $data[] = $row['id'];
    $data[] = $row['name'];

    $query2 = "SELECT l.zipcode, l.latitude, l.longitude FROM locations AS l WHERE storeid=".$row['id'];

    $result2 = mysql_query($query2);
    while($row2 = mysql_fetch_assoc( $result )) {
        $data['locations']['zipcode'] = $row2['zipcode'];
        $data['locations']['latitude'] = $row2['latitude'];
        $data['locations']['longitude'] = $row2['longitude'];
    }
}
否则,您可以使用JOIN获取所有结果,并执行以下操作:

$query = "SELECT * FROM stores AS s 
LEFT JOIN locations AS l
ON s.id = l.storesid";

$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_assoc( $result )) {
    $data[$row[id]]['id'] = $row['id'];
    $data[$row[id]]['name'] = $row['name'];
    $data[$row[id]]['locations'][] = array($row['zipcode'], $row['latitude'], $row['longitude']);
}

但这将使主数组的索引设置为从0开始不连续,但每个索引将等于“存储”项的ID,该项将在所有位置的单独记录中重复存储,与我当前的代码完全相同。我花了一些时间编辑了答案。看一看,将在其中添加更多详细信息。谢谢,伙计,你认为哪一个最快?用第二个。您只有一个SQL查询,其余的由PHP计算,占用的资源更少。干杯
$query = "SELECT * FROM stores AS s 
LEFT JOIN locations AS l
ON s.id = l.storesid";

$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_assoc( $result )) {
    $data[$row[id]]['id'] = $row['id'];
    $data[$row[id]]['name'] = $row['name'];
    $data[$row[id]]['locations'][] = array($row['zipcode'], $row['latitude'], $row['longitude']);
}