[英]Put output in json_encode?
In following php code in here //print_r($reunits);
在下面的PHP代码中
//print_r($reunits);
is output this: http://pastebin.com/RbqZ5kHV 输出如下: http : //pastebin.com/RbqZ5kHV
but in here echo json_encode($reunits);
但在这里
echo json_encode($reunits);
is output as: http://pastebin.com/GFdHkg5Y 输出为: http : //pastebin.com/GFdHkg5Y
If use $reunits = array('reunits'=>$units_data);
如果使用
$reunits = array('reunits'=>$units_data);
as: $reunits .=...
i get this output in echo json_encode($reunits);
如:
$reunits .=...
我在echo json_encode($reunits);
得到此输出echo json_encode($reunits);
: "ArrayArrayArray" : “ ArrayArrayArray”
How can put output like output in //print_r($reunits);
如何将输出像输出一样放在
//print_r($reunits);
on output echo json_encode($reunits);
在输出
echo json_encode($reunits);
? ? How can fix it?
如何解决?
$reunits = "";
//$tourf_id = $this->input->post('tour_name');
$tourf_id = '102';
//$query_r = $this->db->order_by('id','desc')->get_where('tour_foreign_residence', array('relation' => $tourf_id));
$query_r = array('77192276', '15190364', '15183965')
foreach($query_r->result() as $idx=>$val){
$hotel_id = $val->hotel_id;
$query = $this->db->get_where('tour_foreign_units', array('hotel_id' => $hotel_id));
$units_data = array();
foreach ($query->result() as $index=>$row) {
$units_data[] = array(
'name' => $row->name,
'price' => $row->price,
'extra' => $row->extra,
'hotel_id' => $row->hotel_id
);
}
$reunits = array('reunits'=>$units_data);
//print_r($reunits);
}
echo json_encode($reunits);
This output send by json_encode to ajax call in jquery. 此输出通过json_encode发送到jquery中的ajax调用。
If you concat arrays with the string concatenation operator ( .
) , the arrays will be converted into a string (that is "Array"
in PHP) and then concatenated. 如果使用字符串连接运算符(
.
)连接数组,则数组将转换为字符串(PHP中为"Array"
),然后进行连接。
Use an array operator instead: 改用数组运算符 :
$reunits+=...
which will union two arrays. 这将合并两个数组。 If union is not what you're looking for, you can use
array_merge
. 如果union不是您要查找的内容,则可以使用
array_merge
。
Don't forget to initialize the variable at the top as an empty array as well: 不要忘记将顶部的变量也初始化为空数组:
$reunits = array();
If I understand what you are asking, rather than attempting to append onto the array with .=
, you should be using the []
notation to append to the array: 如果我理解您的要求,而不是尝试使用
.=
追加到数组,则应使用[]
表示法追加到数组:
//Initialize reunits as an array
$reunits = array();
foreach($query_r->result() as $idx=>$val){
$hotel_id = $val->hotel_id;
$query = $this->db->get_where('tour_foreign_units', array('hotel_id' => $hotel_id));
$units_data = array();
foreach ($query->result() as $index=>$row) {
$units_data[] = array(
'name' => $row->name,
'price' => $row->price,
'extra' => $row->extra,
'hotel_id' => $row->hotel_id
);
}
// Append the array $units_data onto $reunits
// since $units_data is already an array
$reunits[] = $units_data;
}
// Now the JSON output should look like you expect
echo json_encode($reunits);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.