Wednesday, 15 June 2011

arrays - PHP reformat JSON output -



arrays - PHP reformat JSON output -

i'm building hi score board in construct2 , trying lay out chars correctly.

in order have created array in c2 , using ajax homecoming info json string php script held on webserver. info used populate array utilize fill out spritefonts. simple plenty in theory.

this php file returns json string:

if ($result = $mysqli->query($sql)) { /* fetch associative array */ while ($row = $result->fetch_assoc()) { $array = array( array ( 'name' => $row['pname'], 'score' => $row['score'] )); echo json_encode($array); } /* free result set */ $result->free(); exit(); }

this returns following:

[{"name":"developer","score":"56"}] [{"name":"terrry","score":"34"}] [{"name":"numero_uno","score":"20"}] [{"name":"thomasin :)","score":"18"}] [{"name":"thriftybutstillnifty","score":"18"}] [{"name":"perfect","score":"17"}] [{"name":"bah","score":"17"}] [{"name":"eviledna","score":"16"}] [{"name":"type here","score":"16"}] [{"name":"slaine","score":"14"}]

to used in c2 string needs in format:

{"c2array":true, "size": [2,2,1], "data": [ [["john"],[23]], [["terry"],[43]] ] }

how go creating 1 t'other?

i this:

$response = array( "c2array" => true, "size" => "something", "data" => array() ); while ( $row = $result->fetch_assoc() ) { $response['data'][] = array( array($row['pname']), array($row['score']) ); } echo json_encode($response);

php arrays json construct-2

No comments:

Post a Comment