1

I would like to select a specific line of an object that have been created using json_encode function from a php array.

  while($locations=$req->fetch()){
    $t = $locations->titre;
    $la = $locations->latitude;
    $lo = $locations->longitude;
    $typ = $locations->type;
    $ep = $locations->couleur;
    $matrice[$i] = array($t, $la, $lo, $ep);
    $i=$i+1;
  }

var locations = <?php echo json_encode($matrice); ?>;
locations[0] = ['ma position actuelle', 0, 0, 0];

//console.log(Object.keys(locations));
//console.log(locations);

var centerLat=0.0, centerLong=0.0;
for (var i=1; i<Object.keys(locations).length; i++) {
  centerLat+=locations[i][1];
 centerLong+=locations[i][2];
}

I would like to select the second and the third element of "locations" but the syntax inside the loop is wrong. Does anyone has an idea. Thank you

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Welcome. Please read [ask], particularly how to create an [mcve]. Right now, we have to _guess_ at the data structure. Further, I'm pretty sure you're confusing the matter with all the tags - you likely are just wanting `javascript`, and if you could provide us with the value of `locations`, it'd be far simpler.... – random_user_name Nov 08 '18 at 23:31
  • Is `location` an object or an array? – V. Sambor Nov 08 '18 at 23:32
  • Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – random_user_name Nov 08 '18 at 23:33
  • Try `for (var i in locations) {` instead. – Cobra_Fast Nov 08 '18 at 23:40
  • json_encode results in a string. Your next line is assuming it's an array. Are you sure you didn't mean json_decode? – Forbs Nov 08 '18 at 23:54

2 Answers2

0

First you should do:

var locations = JSON.parse(<?php echo json_encode($matrice); ?>);

Then console.log(locations.toString()); to check your data

After, I think you're looking for Array.prototype.unshift() to add elements at the beginning of the array:

locations.unshift(['ma position actuelle', 0, 0, 0]);

(locations[0] = ['ma position actuelle', 0, 0, 0] just replace first item of the array)

then change you for loop

for (var i=1; i<Object.keys(locations).length; i++)

for

var i = 1, ln = locations.length;
for (i;i<ln;i++)
scraaappy
  • 2,830
  • 2
  • 19
  • 29
0

You can access any item in an JSONArray (or any Array) in JS like this:

object[i]

In your example, if you wanna get the second and third element:

for (...) {
  var longitude = locations[i][1];
  var latitude = locations[i][2];
}

But, I suggest that you use keys and make JSONObjects instead of just JSONArrays, like this:

  $locations = array();

  while($locations=$req->fetch()){

    $location = array(
      'titre' => $locations->titre,
      'latitude' => $locations->latitude,
      'longitude' => $locations->longitude,
      ... etc
    );

    $locations[] = $location;

  }

That way you'll end up with a nice JSONArray filled with JSONObjects and you can call them from JS like this:

//locations is a JSONArray
var locations = <?php echo json_encode($matrice); ?>; 
//locations[0] is a JSONObject
var latitude = locations[0].latitude;
var latitude = locations[0].longitude;
icortesi
  • 760
  • 8
  • 10