為何json_encode()之後,數組裡元素成空值

來源:互聯網
上載者:User
為什麼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 真的有東西?
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.