Q:

convert array into proper JSON format for android using PHP

I am using Codeigniter to fetch rows. Please go through the below code.
//Model

public function selectData() {
    $query = $this->db->query('SELECT * FROM tbl_favourites WHERE favourites_status = 1');
    $count = $query->num_rows();

    if($count > 0)
        return $result = $query->result_array();
    else
        return 0;
}

//Controller

$all_favourites = $this->select->selectData();

if(!empty($all_favourites)) {
    foreach($all_favourites as $favourites)
        echo json_encode($favourites);
}

From the above code, the output is
{"favourite_online_id":"3","favourite_online_url":"ddd","favourite_online_status":"0"}
{"favourite_online_id":"2","favourite_online_url":"http:\/\/www.google.com","favourite_online_status":"0"}

But I want the output to be formatted properly to satisfy the android JSON.
The required output is,
[
  {
    "favourite_online_id": "3",
    "favourite_online_url": "ddd",
    "favourite_online_status": "0"
  },
  {
    "favourite_online_id": "2",
    "favourite_online_url": "http://www.google.com",
    "favourite_online_status": "0"
  }
]

A:

instead of using 
if(!empty($all_favourites)) {
    foreach($all_favourites as $favourites)
        echo json_encode($favourites);
}

use directly
if(!empty($all_favourites)) {

        echo json_encode($all_favourites );
}

