[英]how is output(echo) array without use of json_encode?(codeigniter)
對於 output 數組而不是使用 json_encode,使用什么。(如何在不使用 json_encode 的情況下輸出(echo)數組?)我使用 codeigniter。
CI_控制器:
function auto_complete(){
$hotel_search = $this->input->post('search_hotel');
echo json_encode($this->model_tour->auto_complete($hotel_search));
// if use of json_encode output is '[{"name":"salal"},{"name":"salaso"},{"name":"salasi"},{"name":"salsh"}]' if don want use of json_encode output is "Array"
}
CI_模型:
function auto_complete($hotel_search)
{
$query_hotel_search = $this->db->order_by("id", "desc")->like('name', $hotel_search)->get('hotel_submits');
if($query_hotel_search->num_rows()==0){
return '0';
}else{
$data = array();
foreach ($query_hotel_search->result() as $row)
{
$data[] = array('name' => $row->name);
}
return $data;
}
}
如果您只想查看數組, print_r($array)
或var_dump($array)
將工作
我不確定這是否是您要查找的內容,但它確實有助於顯示數組/JSON 的結構:
echo "<pre>";
print_r($array);
echo "</pre>";
它將顯示斷開的 $array 並保留制表符,以便您查看它是如何嵌套的。
如果要查看數組的內容,請使用print_r
或vardump
如果你想對信息做一些有用的事情,你將不得不遍歷數組
foreach($this->model_tour->auto_complete($hotel_search) as $key => $value){
//do something
}
foreach($array as $key => $value) { echo $key . ' = ' . $value . "\n"; }
這將為您提供作為字符串的數組。
您也可以使用 print_r()
print_r($array);
或 var_dump()
var_dump($array);
您有幾種選擇:
json_encode
(你自己提到的)print_r
或var_dump
(timw4mail 提到了這一點)implode
(但是,這不適用於嵌套的 arrays,它只會顯示鍵)array_map
(使用您自己的函數格式化單個數組元素)和implode
(組合結果)的組合如果您使用 jQuery、any.ajax()、.get() 或 .post(),function 將嘗試猜測從 Z293C9EA246FF9985DC6F62A650F78986 請求返回的數據類型。 將 controller 的內容類型設置為 JSON:
$hotel_search = $this->input->post('search_hotel');
$this->output
->set_content_type('application/json')
->set_output( json_encode($this->model_tour->auto_complete($hotel_search)) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.