PHP JSON Encode not working












1















This is the var_dump of the array I want to encode into JSON:



array(3) { 
[0]=> array(2) {
["From"]=> string(14) "08 August 2013"
["To"]=> string(14) "21 August 2013"
}
[1]=> array(2) {
["From"]=> string(14) "11 August 2013"
["To"]=> string(14) "21 August 2013"
}
[2]=> array(2) {
["From"]=> string(14) "12 August 2013"
["To"]=> string(14) "01 August 2013"
}
}


When I encode it, the output looks like this:



[
{"From":"08 August 2013","To":"21 August 2013"},
{"From":"11 August 2013","To":"21 August 2013"},
{"From":"12 August 2013","To":"01 August 2013"}
]


But I want it to be this:



{
0:{"From":"08 August 2013","To":"21 August 2013"},
1:{"From":"11 August 2013","To":"21 August 2013"},
2:{"From":"12 August 2013","To":"01 August 2013"}
}


It's possible because I've done it before, but using the same code now it won't work










share|improve this question

























  • Try json_encode($array, JSON_FORCE_OBJECT). Not sure if it will work though, honestly.

    – Phas1c
    Aug 2 '13 at 14:21
















1















This is the var_dump of the array I want to encode into JSON:



array(3) { 
[0]=> array(2) {
["From"]=> string(14) "08 August 2013"
["To"]=> string(14) "21 August 2013"
}
[1]=> array(2) {
["From"]=> string(14) "11 August 2013"
["To"]=> string(14) "21 August 2013"
}
[2]=> array(2) {
["From"]=> string(14) "12 August 2013"
["To"]=> string(14) "01 August 2013"
}
}


When I encode it, the output looks like this:



[
{"From":"08 August 2013","To":"21 August 2013"},
{"From":"11 August 2013","To":"21 August 2013"},
{"From":"12 August 2013","To":"01 August 2013"}
]


But I want it to be this:



{
0:{"From":"08 August 2013","To":"21 August 2013"},
1:{"From":"11 August 2013","To":"21 August 2013"},
2:{"From":"12 August 2013","To":"01 August 2013"}
}


It's possible because I've done it before, but using the same code now it won't work










share|improve this question

























  • Try json_encode($array, JSON_FORCE_OBJECT). Not sure if it will work though, honestly.

    – Phas1c
    Aug 2 '13 at 14:21














1












1








1








This is the var_dump of the array I want to encode into JSON:



array(3) { 
[0]=> array(2) {
["From"]=> string(14) "08 August 2013"
["To"]=> string(14) "21 August 2013"
}
[1]=> array(2) {
["From"]=> string(14) "11 August 2013"
["To"]=> string(14) "21 August 2013"
}
[2]=> array(2) {
["From"]=> string(14) "12 August 2013"
["To"]=> string(14) "01 August 2013"
}
}


When I encode it, the output looks like this:



[
{"From":"08 August 2013","To":"21 August 2013"},
{"From":"11 August 2013","To":"21 August 2013"},
{"From":"12 August 2013","To":"01 August 2013"}
]


But I want it to be this:



{
0:{"From":"08 August 2013","To":"21 August 2013"},
1:{"From":"11 August 2013","To":"21 August 2013"},
2:{"From":"12 August 2013","To":"01 August 2013"}
}


It's possible because I've done it before, but using the same code now it won't work










share|improve this question
















This is the var_dump of the array I want to encode into JSON:



array(3) { 
[0]=> array(2) {
["From"]=> string(14) "08 August 2013"
["To"]=> string(14) "21 August 2013"
}
[1]=> array(2) {
["From"]=> string(14) "11 August 2013"
["To"]=> string(14) "21 August 2013"
}
[2]=> array(2) {
["From"]=> string(14) "12 August 2013"
["To"]=> string(14) "01 August 2013"
}
}


When I encode it, the output looks like this:



[
{"From":"08 August 2013","To":"21 August 2013"},
{"From":"11 August 2013","To":"21 August 2013"},
{"From":"12 August 2013","To":"01 August 2013"}
]


But I want it to be this:



{
0:{"From":"08 August 2013","To":"21 August 2013"},
1:{"From":"11 August 2013","To":"21 August 2013"},
2:{"From":"12 August 2013","To":"01 August 2013"}
}


It's possible because I've done it before, but using the same code now it won't work







php arrays json encode var-dump






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 2 '13 at 14:24









Android Developer

9451822




9451822










asked Aug 2 '13 at 14:18









Josh Luke BleaseJosh Luke Blease

56711853




56711853













  • Try json_encode($array, JSON_FORCE_OBJECT). Not sure if it will work though, honestly.

    – Phas1c
    Aug 2 '13 at 14:21



















  • Try json_encode($array, JSON_FORCE_OBJECT). Not sure if it will work though, honestly.

    – Phas1c
    Aug 2 '13 at 14:21

















Try json_encode($array, JSON_FORCE_OBJECT). Not sure if it will work though, honestly.

– Phas1c
Aug 2 '13 at 14:21





Try json_encode($array, JSON_FORCE_OBJECT). Not sure if it will work though, honestly.

– Phas1c
Aug 2 '13 at 14:21












4 Answers
4






active

oldest

votes


















4














Use the : JSON_FORCE_OBJECT argument;



$json = json_encode($array,JSON_FORCE_OBJECT);


That will assign numeric keys






share|improve this answer
























  • also casting the array to object (json_encode((object)$array);) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases

    – x4rf41
    Aug 2 '13 at 14:25





















0














Both versions are equivalent. JS by default will number the array elements starting with 0, so even though they're not specified, you'll still get 0,1,2,.... as the indexes when you decode later on.



You can trivially verify this by decoding the string, e.g.



$array = array('your array here');
$json = json_encode($array);
$decoded_array = json_decode($json);
var_dump($decoded_array);





share|improve this answer































    0














    if you want those index because you want an array of json objects, you don´t need them because you already have that ,if you want to select any of those objects inside of the array you could already use index like:



    example[0].from;



    Every {} means an json object and [{},{},{}] means an array of objects.






    share|improve this answer































      0














      Many Times it occurs when our content of the array is not Encoded. Generally we use UTF-8 Encoding.



      So, To Solve this issue Just add



      mysqli_set_charset($con, 'utf8');



      Just after the connection.






      share|improve this answer























        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',
        autoActivateHeartbeat: false,
        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%2f18019203%2fphp-json-encode-not-working%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        Use the : JSON_FORCE_OBJECT argument;



        $json = json_encode($array,JSON_FORCE_OBJECT);


        That will assign numeric keys






        share|improve this answer
























        • also casting the array to object (json_encode((object)$array);) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases

          – x4rf41
          Aug 2 '13 at 14:25


















        4














        Use the : JSON_FORCE_OBJECT argument;



        $json = json_encode($array,JSON_FORCE_OBJECT);


        That will assign numeric keys






        share|improve this answer
























        • also casting the array to object (json_encode((object)$array);) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases

          – x4rf41
          Aug 2 '13 at 14:25
















        4












        4








        4







        Use the : JSON_FORCE_OBJECT argument;



        $json = json_encode($array,JSON_FORCE_OBJECT);


        That will assign numeric keys






        share|improve this answer













        Use the : JSON_FORCE_OBJECT argument;



        $json = json_encode($array,JSON_FORCE_OBJECT);


        That will assign numeric keys







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 2 '13 at 14:23









        JohnnyFaldoJohnnyFaldo

        2,81521326




        2,81521326













        • also casting the array to object (json_encode((object)$array);) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases

          – x4rf41
          Aug 2 '13 at 14:25





















        • also casting the array to object (json_encode((object)$array);) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases

          – x4rf41
          Aug 2 '13 at 14:25



















        also casting the array to object (json_encode((object)$array);) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases

        – x4rf41
        Aug 2 '13 at 14:25







        also casting the array to object (json_encode((object)$array);) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases

        – x4rf41
        Aug 2 '13 at 14:25















        0














        Both versions are equivalent. JS by default will number the array elements starting with 0, so even though they're not specified, you'll still get 0,1,2,.... as the indexes when you decode later on.



        You can trivially verify this by decoding the string, e.g.



        $array = array('your array here');
        $json = json_encode($array);
        $decoded_array = json_decode($json);
        var_dump($decoded_array);





        share|improve this answer




























          0














          Both versions are equivalent. JS by default will number the array elements starting with 0, so even though they're not specified, you'll still get 0,1,2,.... as the indexes when you decode later on.



          You can trivially verify this by decoding the string, e.g.



          $array = array('your array here');
          $json = json_encode($array);
          $decoded_array = json_decode($json);
          var_dump($decoded_array);





          share|improve this answer


























            0












            0








            0







            Both versions are equivalent. JS by default will number the array elements starting with 0, so even though they're not specified, you'll still get 0,1,2,.... as the indexes when you decode later on.



            You can trivially verify this by decoding the string, e.g.



            $array = array('your array here');
            $json = json_encode($array);
            $decoded_array = json_decode($json);
            var_dump($decoded_array);





            share|improve this answer













            Both versions are equivalent. JS by default will number the array elements starting with 0, so even though they're not specified, you'll still get 0,1,2,.... as the indexes when you decode later on.



            You can trivially verify this by decoding the string, e.g.



            $array = array('your array here');
            $json = json_encode($array);
            $decoded_array = json_decode($json);
            var_dump($decoded_array);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 2 '13 at 14:21









            Marc BMarc B

            315k31323426




            315k31323426























                0














                if you want those index because you want an array of json objects, you don´t need them because you already have that ,if you want to select any of those objects inside of the array you could already use index like:



                example[0].from;



                Every {} means an json object and [{},{},{}] means an array of objects.






                share|improve this answer




























                  0














                  if you want those index because you want an array of json objects, you don´t need them because you already have that ,if you want to select any of those objects inside of the array you could already use index like:



                  example[0].from;



                  Every {} means an json object and [{},{},{}] means an array of objects.






                  share|improve this answer


























                    0












                    0








                    0







                    if you want those index because you want an array of json objects, you don´t need them because you already have that ,if you want to select any of those objects inside of the array you could already use index like:



                    example[0].from;



                    Every {} means an json object and [{},{},{}] means an array of objects.






                    share|improve this answer













                    if you want those index because you want an array of json objects, you don´t need them because you already have that ,if you want to select any of those objects inside of the array you could already use index like:



                    example[0].from;



                    Every {} means an json object and [{},{},{}] means an array of objects.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 2 '13 at 14:27









                    Jao AssyJao Assy

                    246




                    246























                        0














                        Many Times it occurs when our content of the array is not Encoded. Generally we use UTF-8 Encoding.



                        So, To Solve this issue Just add



                        mysqli_set_charset($con, 'utf8');



                        Just after the connection.






                        share|improve this answer




























                          0














                          Many Times it occurs when our content of the array is not Encoded. Generally we use UTF-8 Encoding.



                          So, To Solve this issue Just add



                          mysqli_set_charset($con, 'utf8');



                          Just after the connection.






                          share|improve this answer


























                            0












                            0








                            0







                            Many Times it occurs when our content of the array is not Encoded. Generally we use UTF-8 Encoding.



                            So, To Solve this issue Just add



                            mysqli_set_charset($con, 'utf8');



                            Just after the connection.






                            share|improve this answer













                            Many Times it occurs when our content of the array is not Encoded. Generally we use UTF-8 Encoding.



                            So, To Solve this issue Just add



                            mysqli_set_charset($con, 'utf8');



                            Just after the connection.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 '18 at 6:38









                            Dharmesh PatelDharmesh Patel

                            12




                            12






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18019203%2fphp-json-encode-not-working%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







                                這個網誌中的熱門文章

                                Xamarin.form Move up view when keyboard appear

                                Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                Anylogic : not able to use stopDelay()