Jump to content

add/edit/delete json array


Recommended Posts



i have on row in database with this:


to read the data, i use :

foreach ($datasortarray as $key=>$row){
 echo $key ;

first is the name of the city, every city has an array with data - number seats, latitude, longitude

Can you please tell me how to add/delete other data to this array?

if i have these input fields:

<input name="city" type="text"> -> city name
<input name="numbers" type="text"> -> for first number..
<input name="latitude" type="text"> -> for second number
<input name="longitude" type="text"> -> for third number

Link to comment
Share on other sites

//to add or change
$datasortarray->{$_POST['city']}[0] = $_POST['numbers'];
$datasortarray->{$_POST['city']}[1] = $_POST['latitude'];
$datasortarray->{$_POST['city']}[2] = $_POST['longitude'];

$datasortarray->{$_POST['city']} = array($_POST['numbers'], $_POST['latitude'], $_POST['longitude']);


Link to comment
Share on other sites

jazzman1, i wanted to create a way to save different information about different cities in one field, because when there are many users in one table, and if there are many information about cities for each user .. the rows will be much more.

So in this way you have the whole information in one field:)

Maybe there is and another way, but i dont know it.

Link to comment
Share on other sites

But, you have already saved this information, everything you have to do, is to display this JSON object in proper readable format!


To keep the things simple, let's say we have two files:

json.php ( it's a file that we're connecting to our database and retrieve our database data into a JSON object)

display.php (it's a file that we want to display JSON) 


So, the conetent is:



echo '{"Sofia":["26","42.696693","23.302080"],"Lovetch":["13","43.140461","24.718996"]}';


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript">
     $.getJSON('json.php',null, processJSON);
      function processJSON(data){
      var infoHtml = '';
      $.each(data, function(city, cityInfo) {
       infoHtml += '<h4> City: '+city+'</h4>'
       infoHtml += '<p>Numbers: '+cityInfo[0]+'</p>'
       infoHtml += '<p>Latitude: '+cityInfo[1]+'</p>'
       infoHtml += '<p>Longitude '+cityInfo[2]+'</p>'
       }); //end each loop
       //display infoHtml variable
  <div id="infoBox"></div>

So, if we run the script, the result shoud be someting like:



City: Sofia

Numbers: 26

Latitude: 42.696693

Longitude 23.302080

City: Lovetch

Numbers: 13

Latitude: 43.140461

Longitude 24.718996


If we want to edit(update) or delete some particular row in our database, we should have to create only two html input fields (or links) and each of them can provide some paricular information itself to delete or update something in our database. Send this request by Ajax and everything should be Ok. That's all you have to do.  


PS:  If you want to add a new info, you have to create an html form to do this. Just forgot to mention :)

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.