how to use json_encode function of an array
up vote
1
down vote
favorite
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
javascript php json
add a comment |
up vote
1
down vote
favorite
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
javascript php json
Welcome. Please read How to Ask, particularly how to create an Minimal, Complete, and Verifiable example. 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 wantingjavascript, and if you could provide us with the value oflocations, it'd be far simpler....
– cale_b
Nov 8 at 23:31
Islocationan object or an array?
– V. Sambor
Nov 8 at 23:32
Possible duplicate of Find object by id in an array of JavaScript objects
– cale_b
Nov 8 at 23:33
Tryfor (var i in locations) {instead.
– Cobra_Fast
Nov 8 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 8 at 23:54
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
javascript php json
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
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;
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];
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];
javascript php json
javascript php json
edited Nov 8 at 23:33
GolezTrol
97.1k9129171
97.1k9129171
asked Nov 8 at 23:29
Mehdi Beriane
61
61
Welcome. Please read How to Ask, particularly how to create an Minimal, Complete, and Verifiable example. 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 wantingjavascript, and if you could provide us with the value oflocations, it'd be far simpler....
– cale_b
Nov 8 at 23:31
Islocationan object or an array?
– V. Sambor
Nov 8 at 23:32
Possible duplicate of Find object by id in an array of JavaScript objects
– cale_b
Nov 8 at 23:33
Tryfor (var i in locations) {instead.
– Cobra_Fast
Nov 8 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 8 at 23:54
add a comment |
Welcome. Please read How to Ask, particularly how to create an Minimal, Complete, and Verifiable example. 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 wantingjavascript, and if you could provide us with the value oflocations, it'd be far simpler....
– cale_b
Nov 8 at 23:31
Islocationan object or an array?
– V. Sambor
Nov 8 at 23:32
Possible duplicate of Find object by id in an array of JavaScript objects
– cale_b
Nov 8 at 23:33
Tryfor (var i in locations) {instead.
– Cobra_Fast
Nov 8 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 8 at 23:54
Welcome. Please read How to Ask, particularly how to create an Minimal, Complete, and Verifiable example. 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....– cale_b
Nov 8 at 23:31
Welcome. Please read How to Ask, particularly how to create an Minimal, Complete, and Verifiable example. 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....– cale_b
Nov 8 at 23:31
Is
location an object or an array?– V. Sambor
Nov 8 at 23:32
Is
location an object or an array?– V. Sambor
Nov 8 at 23:32
Possible duplicate of Find object by id in an array of JavaScript objects
– cale_b
Nov 8 at 23:33
Possible duplicate of Find object by id in an array of JavaScript objects
– cale_b
Nov 8 at 23:33
Try
for (var i in locations) { instead.– Cobra_Fast
Nov 8 at 23:40
Try
for (var i in locations) { instead.– Cobra_Fast
Nov 8 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 8 at 23:54
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 8 at 23:54
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
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++)
add a comment |
up vote
0
down vote
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;
It works perfectly thank you very much !
– Mehdi Beriane
Nov 9 at 10:33
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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++)
add a comment |
up vote
0
down vote
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++)
add a comment |
up vote
0
down vote
up vote
0
down vote
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++)
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++)
edited Nov 9 at 0:00
answered Nov 8 at 23:45
scraaappy
2,17121325
2,17121325
add a comment |
add a comment |
up vote
0
down vote
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;
It works perfectly thank you very much !
– Mehdi Beriane
Nov 9 at 10:33
add a comment |
up vote
0
down vote
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;
It works perfectly thank you very much !
– Mehdi Beriane
Nov 9 at 10:33
add a comment |
up vote
0
down vote
up vote
0
down vote
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;
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;
answered Nov 9 at 1:38
icortesi
51455
51455
It works perfectly thank you very much !
– Mehdi Beriane
Nov 9 at 10:33
add a comment |
It works perfectly thank you very much !
– Mehdi Beriane
Nov 9 at 10:33
It works perfectly thank you very much !
– Mehdi Beriane
Nov 9 at 10:33
It works perfectly thank you very much !
– Mehdi Beriane
Nov 9 at 10:33
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217702%2fhow-to-use-json-encode-function-of-an-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome. Please read How to Ask, particularly how to create an Minimal, Complete, and Verifiable example. 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 oflocations, it'd be far simpler....– cale_b
Nov 8 at 23:31
Is
locationan object or an array?– V. Sambor
Nov 8 at 23:32
Possible duplicate of Find object by id in an array of JavaScript objects
– cale_b
Nov 8 at 23:33
Try
for (var i in locations) {instead.– Cobra_Fast
Nov 8 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 8 at 23:54