PHP - how do I return JSON in a specific format I need? -
i have php file returns json in these 2 ways:
if error happens, this:
$post_data = array('error' => "no_member_id"); echo json_encode($post_data); and if there no error, , need return data in json format, this:
if (mysql_num_rows($result) > 0 ) { $rows = array(); while($r = mysql_fetch_assoc($result)) { $rows[] = $r; } echo json_encode($rows); } but need return data in format this:
{"result":"ok", data :[{"data1":"value1", "data2":"value2"}]} or this:
{"result":"error", data :[{"error":"no_id"}]} could please me understand how that?
thanks!!
echo json_encode( array( "result" => "ok", "data" => $rows ) ); instead of
echo json_encode($rows);
Comments
Post a Comment