ace5342's Forum Posts

  • 4 posts
  • Hi i posted this at the end of my last post but figure i should make a separate post for it.

    The problem is i have loaded data from json into an array and it all works fine all the data is there but when i call it out

    eg. if my array is 2 x 1 x 1

    [

    [First],[Second]]

    if i call the 1st one in the array

    MYARRAY.At(0)

    and set the text of a textbox to that data i only get the first letter of the data i call in this case F and not First as it should be if i call MYARRAY.At(1) i get S not Second?

    i know i have all the info in the array as if i call all the data out of the array using asJSON and send it to the textbox it prints it all out eg. [[First],[Second]]

    [SOLUTION BELOW]

    input array needed another set of [ ] around each item so

    THIS -> [[First],[Second]] -> BECOMES THIS -> [[[First]],[[Second]]]

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

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

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

  • 4 posts