Why does elements in the array become null after json_encode ()?

Source: Internet
Author: User
Why do elements in the array become null after json_encode? PHPcode $ data = array (& quot; order_table & quot; = & gt; $ order_table, & quot; why does the element in the array become null after json_encode?
PHP code
   $data           =   array("order_table" => $order_table,"html"=>$html);


Html is the html code of a table.
Through js
JScript code
  eval("(" + xmlHttp.responseText + ")");

After that, sometimes the html variable becomes null, but order_table does not,

In php:
PHP code
  $data  =  json_decode($data);print_r($data );

Html is also null!

Why ???? Not solved in the afternoon... the expert gave me some advice.

------ Solution --------------------
Js can only serialize lightweight arrays in json format, while multidimensional arrays in json format cannot be converted.
------ Solution --------------------
$ Data = array ("order_table" => $ order_table, "html" => $ html );
Your $ data is just a php array, $ data = json_decode ($ data); how can I get the result?
------ Solution --------------------
Will it be converted to UTF-8 first?
------ Solution --------------------
Multi-dimensional arrays can also be converted. you can use a browser debugging tool to track the returned content of your request.


------ Solution --------------------
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' => "deleted successfully "));}} else {echo json_encode (array ('message' => "failed to delete "));}}
------ Solution --------------------
This is not a problem with multi-dimensional arrays.

I think it is the reason why GB2312 contains Chinese characters.

UTF8

Try to use English or UTF8 format for all the HTML files to see what the output is.

PHP code
$ Data = array ("order_table" => $ order_table, "html" => iconv ("GBK", "UTF-8 // IGNORE", $ html ));
------ Solution --------------------
PHP code
$ Data = array ("order_table" => $ order_table, "html" => $ html); exit (json_encode ($ data ));
------ Solution --------------------
In this case, you have to make mistakes... $ html really has something?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.