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 9266997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:26:50+00:00 2026-06-18T14:26:50+00:00

I’m trying to create a associative array using PHP. I’m using a ajax script

  • 0

I’m trying to create a associative array using PHP. I’m using a ajax script that calls a json file.

I have tested the script using the following PHP code:

$testLocs = array(
'loc5' => array( 'info' => 'Some random info', 'lat' => 0, 'lng' => 60 ),
'loc6' => array( 'info' => 'Some random info', 'lat' => 0, 'lng' => 40.345 )
);
echo json_encode($testLocs);

which echos:

{"loc1":{"info":"Some random info","lat":0,"lng":60},"loc1":{"info":"Some random info","lat":0,"lng":40.345}}

The ajax code will work correctly. Now I’m trying to write a PHP script that will get the info from the database. My PHP code is below

$query = "Select * from table";
$result = mysql_query($query);

$json_data = array();

while ($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
$lat = $row['lat'];
$lon = $row['lon'];
$page = $row['page'];
array_push($json_data, array('info' => 'Some random info', 'lat' => $lat, 'lng' => $lon)); 
}


echo json_encode($json_data);

which echos:

{"info":"Some random info","lat":"-31.9522","lng":"115.8614"},{"info":"Some random info","lat":"40.7842","lng":"-73.8422"}

I cannot figure out how to put 'loc' => in front of each array. The ajax is using The locNUM as a unique ID.

Thank you

  • 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-06-18T14:26:51+00:00Added an answer on June 18, 2026 at 2:26 pm

    try

    $json_data = array();
    $i = 0;
    
    while ($row = mysql_fetch_assoc($result)) {
    $id = $row['id'];
    $lat = $row['lat'];
    $lon = $row['lon'];
    $page = $row['page'];
    $json_data["loc" . ($i++)] = array('info' => 'Some random info', 'lat' => $lat, 'lng' => $lon); 
    }
    

    instead of array_push.
    array_push is meant as a pure array function, not as a hash manipulating function.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am confused How to use looping for Json response Array in another Array.
I am using JSon response to parse title,date content and thumbnail images and place

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.