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?