Rectangle 27 0

php creating a json API request response from data stored in MySql?


<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);

//Output: {"a":1,"b":2,"c":3,"d":4,"e":5}
?>

Build an array. When you're done, just use json_encode($array) to output as correct valid json format.

Doing this will secure your code, you wont get unexpected results - as it's much easier to deal with arrays compared to json-formatted code.

Note