Accepted answer

this will return a json object with only one result.

if we use previous example, can be done like this:

$arr = $this->company->find('all'); // fetch the array
$arr1 = array();
foreach ($arr as $value) {
    $tmp = array();

    $tmp['name'] = $value['company']['nome'];
    $tmp['data'] = count($value['branch']);

    $arr1[] = $tmp;
return json_encode($arr1);


the whole idea is about like this

$arr=$this->company->find('all'); // fetch the array
foreach ($arr as $value) {
    //some more manual transform to desire format
return json_encode($arr1);


 $sql=mysql_query("select * from posts limit 20"); 

 $response = array();
 $posts = array();

    $posts[] = array('title'=> $title, 'url'=> $url);


    $response['posts'] = $posts;

    $fp = fopen('results.json', 'w');
    fwrite($fp, json_encode($response));


this will generate a file called results.json file where your php file is stored on your online server, taking url and title variables from your mysql db, you can change the variable names to what you want to fetch.

Related Query

More Query from same tag