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










share|improve this question























  • 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










  • 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














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










share|improve this question























  • 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










  • 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












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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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










  • 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
















  • 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










  • 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















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












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





share|improve this answer





























    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;





    share|improve this answer




















    • It works perfectly thank you very much !
      – Mehdi Beriane
      Nov 9 at 10:33










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























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





    share|improve this answer


























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





      share|improve this answer
























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





        share|improve this answer














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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 0:00

























        answered Nov 8 at 23:45









        scraaappy

        2,17121325




        2,17121325






















            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;





            share|improve this answer




















            • It works perfectly thank you very much !
              – Mehdi Beriane
              Nov 9 at 10:33














            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;





            share|improve this answer




















            • It works perfectly thank you very much !
              – Mehdi Beriane
              Nov 9 at 10:33












            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;





            share|improve this answer












            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;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 9 at 1:38









            icortesi

            51455




            51455











            • 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




            It works perfectly thank you very much !
            – Mehdi Beriane
            Nov 9 at 10:33

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            How do I collapse sections of code in Visual Studio Code for Windows?

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ