首頁 >後端開發 >php教程 >php json_encode

php json_encode

WBOY
WBOY原創
2016-08-08 09:27:121325瀏覽

PHP json_encode() 函數介紹


在 php 中使用 json_encode() 內建它很好的資料傳遞並且可以使用它來使用 php 中與它很好的資料。

這個函數的功能是將數值轉換成json資料儲存格式。

01<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php $arr
 = 

array<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none> <codecourier new min-height:inherit background:none><codecourier new font-weight:bold min-height:inherit color:rgb background:none>03</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody>       </table>(<p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>04<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>          <codecourier new min-height:inherit color:blue background:none>'Name'<codecourier new min-height:inherit background:none><codecourier new min-height:inherit color:blue background:none> <codecourier new min-height:inherit background:none></codecourier></codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table>05<tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>          ' Age'<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>=>22<codecourier new min-height:inherit color:blue background:none><codecourier new min-height:inherit background:none></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody> </table> <p></p> <table>06<tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none>); <codecourier new min-height:inherit display:block background:none><p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>07<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none> </tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody> json_encode(<tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none>$arr<codecourier new min-height:inherit display:block background:none>);<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none><codecourier new min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none><codecourier new min-height:inherit background:none> </codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr> </tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>10<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>?></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p>1</p> <divcourier new min-height:inherit><p></p> <p>{</p> <table><tbody>"Name"<tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none> :null,<codecourier new min-height:inherit display:block background:none>"Age"<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none>:20}<codecourier new min-height:inherit background:none><codecourier new min-height:inherit color:blue background:none><codecourier new min-height:inherit background:none><codecourier new min-height:inherit color:blue background:none><codecourier new min-height:inherit background:none>json_encode 函數中緊密地編碼成null 了, json_encode 函數中緊密地編碼成null 了, </codecourier></codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr> 8 編碼,我認為是前端的Javascript 也是utf-8 的原因。 </tbody></table> <p></p> <divcourier new min-height:inherit><p>1</p> <p></p> <table><tbody><tr> <?php <tdCourier New',Courier,monospace!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:top!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:3em!important; line-height:1.1em!important; min-height:inherit!important; color:rgb(175,175,175)!important; background:none!important"><codecourier new min-height:inherit display:block background:none><tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php
🎜

2$數組 3個

    

(

4        'name' 8','你妹') ,
5

    

6    );
);

 

8

迴聲 json_encode($數組);

9?>

1{"title" :"u6d63u72b2ue785","body":
   iconv 轉換格式時用GBK,gb2312 報錯問題,是因為GB2312只收錄6763個漢字,出現未收錄的漢字時,iconv報錯。 GBK向下完全相容於GB2312-80,支援更多的字,但仍然會出現錯誤,解決方法是用GB18030,GB18030基本相容GBK,又擴充了百餘字型。

數組中所有中文在json_encode之後都不見了或出現u6d63等。

解決方法是用urlencode()函數處理以下,在json_encode之前,把所有數組內所有內容都用urlencode()處理一下,然用json_encode()轉換成json字串,最後再用urldecode()將編碼過的中文轉回來。

01<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php /*************** ***********************************************

03 
*

 *

04 *  使用特定function對陣列中所有元素做處理

05 05* @param  string  &$array     要處理的字串

@param  string  $function   要執行的函數

07 *  @return boolean $apply_to_keys_also     是否也應用在key上

088* @access public

09 

10 ************************************ *************************/

11函數  $數組$函數$apply_to_keys_also = 假)

12{

13    靜態 $recursive_? 0;

14   >; 1000) {

15        '可能會死'可能會死 深度遞歸攻擊');

16

17    foreach (foreach (  $價值) {

18     () $價值)) {

19          地], $函數$apply_to_keys_also); 20
    &nbs
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn