我怎样才能json_encode这个查询的结果?

时间:2011-09-26 23:41:13

标签: php codeigniter json

foreach($hello_world as $key=>$product_id) {
if(in_array($key,$exclude))continue;

$query = $this->db->query("SELECT * FROM product where modelNumber = '$product_id'");
if ($query->num_rows() > 0) {
foreach($query->result() as $row) {
    echo $row->name;
    }
}
}

有人可以就如何json编码此查询的结果给我建议吗? 感谢

1 个答案:

答案 0 :(得分:5)

$data = array();
$query = $this->db->query("SELECT * FROM product where modelNumber = '$product_id'");
if ($query->num_rows() > 0) {
foreach($query->result() as $row) {
    $data[$row->id] = $row->name;
    }
}
$json = json_encode($data);