[英]how is output(echo) array without use of json_encode?(codeigniter)
for output array Instead of using json_encode, use of what.(how is output(echo) array without use of json_encode?) i use of codeigniter.对于 output 数组而不是使用 json_encode,使用什么。(如何在不使用 json_encode 的情况下输出(echo)数组?)我使用 codeigniter。
CI_Controller: 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_model: 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;
}
}
If you just want to view the array, print_r($array)
or var_dump($array)
will work如果您只想查看数组, print_r($array)
或var_dump($array)
将工作
I'm not sure if this is what you are looking for, but it really helps show the structure of the array/JSON:我不确定这是否是您要查找的内容,但它确实有助于显示数组/JSON 的结构:
echo "<pre>";
print_r($array);
echo "</pre>";
It will show the $array broken out and will keep the tabbing so you can see how it's nested.它将显示断开的 $array 并保留制表符,以便您查看它是如何嵌套的。
If you want to view the contents of the array, use print_r
or vardump
如果要查看数组的内容,请使用print_r
或vardump
If you want to do something useful with the information, you're going to have to loop through the array如果你想对信息做一些有用的事情,你将不得不遍历数组
foreach($this->model_tour->auto_complete($hotel_search) as $key => $value){
//do something
}
foreach($array as $key => $value) { echo $key . ' = ' . $value . "\n"; }
That will give you the array as string.这将为您提供作为字符串的数组。
You can also use print_r()您也可以使用 print_r()
print_r($array);
or var_dump()或 var_dump()
var_dump($array);
You have a few options at your disposal:您有几种选择:
json_encode
(which you mention yourself) json_encode
(你自己提到的)print_r
or var_dump
(timw4mail mentions this) print_r
或var_dump
(timw4mail 提到了这一点)implode
(however, this won't work for nested arrays, and it will only display the keys)内implode
(但是,这不适用于嵌套的 arrays,它只会显示键)array_map
(to format individual array elements using your own function) and implode
(to combine the results) array_map
(使用您自己的函数格式化单个数组元素)和implode
(组合结果)的组合If you're using jQuery, any.ajax(), .get(), or.post() function will try to guess the data type being returned from the HTTP request.如果您使用 jQuery、any.ajax()、.get() 或 .post(),function 将尝试猜测从 Z293C9EA246FF9985DC6F62A650F78986 请求返回的数据类型。 Set the content type of your controller to JSON:将 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.