score:1

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);

score:0

the whole idea is about like this

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

score:1

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

 $response = array();
 $posts = array();
  while($row=mysql_fetch_array($sql)) 
  { 
    $title=$row['title']; 
    $url=$row['url']; 

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

  } 

    $response['posts'] = $posts;

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


  ?> 

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