make json array with php [SOLVED]

0 favourites
  • 8 posts
From the Asset Store
Paypal PHP Supports multiple platforms, Web/html5 Android, IOS. (Including storing transaction data)
  • Hi i have been looking through the forums and can't find anything about making a json array with php that prints anything like the json i have seen here?

    This is my php that prints valid json.

    $json_data = array();

    while ($row = mysql_fetch_assoc($sql)) {

       array_push($json_data, $row);

    }

    $json_data = json_encode($json_data);

    echo $json_data;

    The output look like this.

    [{"id1":"data1","id2":"data2","id3":"data3","id4":"data4","id5":"data5",""id6":"data6","id7":"data7","id8":"data8","id9":"data9","id10":"data10","id11":"data11","id12":"data12","id13":"data13"}]

    After this i make an ajax request for the page.

    This dose not work all the json arrays i have seen have ([]) not ("") how can i get php to format it like it should be for construct.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Beyond the scope of the forums here, really.

    I'd suggest if you're using php 5.2 or above to use Json Encode and if you aren't, then do a google search for PHP Json encode function. There are plenty out there you can cut and paste that will properly convert a php array into json.

  • Yes i am using php 5.2 and json_encode the data I get out is json but it's not formated in the way construct 2 uses. It look like the json_encode encodes the data one way and construct wants it to be formatted in a different way. All the posts I have look at about json_encode the data come out looking the same as I have posted above?

  • You can't just load a json_encoded array into a C2 array. It has to be in a specific format for C2.

    {"c2array":true,"size":[5,1,1],"data":[[[0]],[[0]],[[0]],[[0]],[[0]]]}

  • Or you use the Hash plugin it got JSON functions

    scirra.com/forum/plugin-hash-table_topic47637_page1.html

  • Thanks ramones I know about that forgot to include it in my post but the problem was the format of the array after theubie's post i looked at php.net about json_encode the php needs to be formated in a certain way for json to encode it as I wanted.

    Thanks for both responses.

    The soloution is when building the array you have to put it into an array like so

    $json_data = array();

    while ($row = mysql_fetch_assoc($sql)) {

       $json_data = array(array(array(FIRST BIT OF DATA)), array(array(SECOND BIT OF DATA)), array(array(NEXT BIT OF DATA)), array(array(AND SO ON)));

    }

    $json_data = json_encode($json_data);

    echo '{"c2array":true,"size":[4,1,1],"data":';

    echo $json_data;

    echo '}';

    this example will ouput for a 4 x 1 x 1 array that with a json load in construct 2 will work and load your data in for you.

    Sorry localboss did not see your comment there till i come to edit this must have been typing my original out as you commented thanks for the suggestion i have managed to solve this now

  • Thanks a ton !! ace5342...I had similar problem. your solution worked out well.. <img src="smileys/smiley1.gif" border="0" align="middle" />

  • Thanks ramones I know about that forgot to include it in my post but the problem was the format of the array after theubie's post i looked at php.net about json_encode the php needs to be formated in a certain way for json to encode it as I wanted.

    Thanks for both responses.

    The soloution is when building the array you have to put it into an array like so

    $json_data = array();

    while ($row = mysql_fetch_assoc($sql)) {

       $json_data = array(array(array(FIRST BIT OF DATA)), array(array(SECOND BIT OF DATA)), array(array(NEXT BIT OF DATA)), array(array(AND SO ON)));

    }

    $json_data = json_encode($json_data);

    echo '{"c2array":true,"size":[4,1,1],"data":';

    echo $json_data;

    echo '}';

    this example will ouput for a 4 x 1 x 1 array that with a json load in construct 2 will work and load your data in for you.

    Sorry localboss did not see your comment there till i come to edit this must have been typing my original out as you commented thanks for the suggestion i have managed to solve this now ace53422013-02-18 21:31:29

    I have scoured this forum for this solution for days now - thanks so much for sharing!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)