Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6124205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:06:22+00:00 2026-05-23T16:06:22+00:00

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

  • 0

Table stores

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

Table locations

storeid  zipcode  latitude  longitude
1        90001    xxxxx     xxxxx
1        45802    xxxxx     xxxxx
2        32843    xxxxx     xxxxx

How can i produce an associative array that contains a key called locations which is an array of all locations of a store?

My current SQL ( which separate each location in a record ):

SELECT stores.*, locations.* FROM locations INNER JOIN stores ON stores.id = locations.storeid

Example of what i want:

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
                                   )
                          ) 
         )
)

and so on for other stores…

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:06:23+00:00Added an answer on May 23, 2026 at 4:06 pm

    So you can’t extract the data in one query because SQL normally works per row and hasn’t got data structure like PHP arrays. You can’t nest the records using JOIN. That’s why you’ll have to do it with with separate queries in a PHP loop. Like so:

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

    Otherwise you can grab all results with JOIN and do as follows:

    $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']);
    }
    

    But this will make the main array’s index to be set not sequential starting from 0 but each index will be equal to the ID of the “store” item

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table User that stores user information - such as name, date
In mysql, I got table scores as follow Id Date Name score 1 2011-08-24
I have a logging table that stores the user id, date/time, table name, record
I have the following data: Table Name: NODE NID | Name | Date 001
I am passing table field name as parameter to stored procedure but stored procedure
I am here to change the uppercase table name present in the stored procedures.
I have a MSSQL table stores that has the following columns in a table:
I have a table that stores user information. The table has a userid (identity)
I need a table that stores key-value pairs, so I created one with a
I have a table that stores various clients I have done work for, separated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.