為什麼json_encode()之後,數組裡元素成空值?
PHP code
$data = array("order_table" => $order_table,"html"=>$html);
html是table表格html代碼
通過js
JScript code
eval("(" + xmlHttp.responseText + ")");
之後,有時候html變數成空值,order_table卻沒有,
在php情況下:
PHP code
$data = json_decode($data);print_r($data );
html也是空值!
這是為什麼呢????一下午沒解決.....那位高手幫忙指點
------解決方案--------------------
js只能json序列化輕量型的數組,多維陣列json轉不了。
------解決方案--------------------
$data = array("order_table" => $order_table,"html"=>$html);
你的 $data 不過是一個 php 數組,$data = json_decode($data); 如何能得到結果?
------解決方案--------------------
會不會要先轉utf-8?
------解決方案--------------------
多維陣列也是可以轉化的,你可以通過瀏覽器調試工具跟蹤一下,看看你的這個請求發出去到底返回了什麼。
------解決方案--------------------
PHP code
public function removeMenuItemAction(){ $request = $this->getRequest(); $layout = $this->_helper->layout(); $layout->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); $menuId = $request->getParam('menuId', null); if( $menuId > 0 ){ $SQLTool = new MysqlTool(); $con = $SQLTool->getConnect(); if( $con ){ $sqlLineMenu = "select Id FROM menubar where Type=0"; $resultMenu = mysql_query($sqlLineMenu); $rowMenubar = mysql_fetch_array($resultMenu,MYSQL_ASSOC); $menubarId = $rowMenubar['Id']; // remove his children $this->callBackMenuitemDelete($menuId,$menubarId); $sqlLine = "DELETE FROM menuitem WHERE Id = " . $menuId; mysql_query($sqlLine); mysql_close($con); echo json_encode(array('message'=>"刪除成功")); } }else{ echo json_encode(array('message'=>"刪除失敗")); } }
------解決方案--------------------
這個不是多維陣列的問題吧
感覺是含有 GB2312 的中文的原因
用UTF8
試試把HTML全部用英文或者UTF8格式,看看輸出什麼
PHP code
$data = array("order_table" => $order_table,"html" => iconv("GBK","UTF-8//IGNORE",$html));
------解決方案--------------------
PHP code
$data = array("order_table" => $order_table,"html"=>$html);exit(json_encode($data));
------解決方案--------------------
這種情況,只能是你手誤了... $html 真的有東西?