ホームページ  >  記事  >  バックエンド開発  >  php json_encode

php json_encode

WBOY
WBOYオリジナル
2016-08-08 09:27:121262ブラウズ

PHP json_encode() 関数の紹介


php (php > 5.2) の json_encode() 組み込み関数を使用すると、php のデータを他の言語にうまく転送できます。そして使用されました。

この関数の機能は、数値をjsonデータの保存形式に変換することです。

01<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><tdcourier new rgb><codecourier new important><?php01<?php

02$arr = array

03 (
🎜🎜🎜🎜🎜02🎜$arr = 配列🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜03🎜 (コード>コード>🎜🎜🎜🎜</p><p><table><tbody><tr><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',Courier,monospace!重要; min-height:継承!重要;表示:ブロック!重要;背景:なし!重要">0404 'Name'=>'你妹',

05          'Age'=>22

06       ); 'Name' =>'你妹' ,
🎜🎜🎜🎜🎜🎜05🎜 '年齢'=>22🎜🎜 🎜🎜🎜🎜🎜🎜🎜🎜06🎜 );🎜🎜🎜🎜

0707 

08$jsonencode 08$arr);

09echo $jsonencode;$jsonencode= json_encode(
$arr🎜🎜);🎜🎜🎜🎜 🎜🎜🎜🎜🎜🎜09🎜🎜echo🎜 $jsonencode🎜🎜;🎜🎜🎜🎜🎜

1010?>

程序运行结果如下:

1{"Name":null,"Age":20}

json_encode 函数中中文被编码成 null 了,Google 了一下,很简单,为了与前端紧密结合,Json 只支持 utf-8 编码,我认为是前端的 Javascript 也是 utf-8 的原因。

1<?php?>
🎜🎜🎜🎜程序运行结果如下:🎜🎜🎜 🎜🎜🎜🎜1🎜{"名前":null,"Age":20}🎜🎜🎜🎜🎜🎜🎜🎜json_encode関数数中中文被编码成 null 了,Google 了一下,很简单,為了与前端紧密结合,Json のみ utf- をサポート8 暗号、フロントエンドの Javascript も utf-8 の原因であると考えています。等幅!重要; min-height:継承!重要;表示:ブロック!重要;背景:なし!重要">1🎜<?php🎜🎜🎜🎜

22$array = array

3 ($array = 配列
🎜🎜🎜🎜🎜🎜3🎜 (🎜🎜🎜🎜

44 'name'=>iconv('GB18030','utf-8','你妹'),

5        'sex'=>'men' '名前' =>iconv('GB18030 ', 'utf-8' ,'你妹'),
🎜🎜🎜🎜🎜5🎜 'sex'=>「男性」🎜🎜🎜🎜

66    );

7 

8echo json_encode($array); );
🎜🎜🎜 🎜🎜🎜7🎜8🎜echo json_encode($array);🎜🎜🎜🎜

99?>

这个程序的运行结果为:

1{"title":"u6d63u72b2ue785","body":"men"}?>
🎜🎜🎜🎜この这个程序の実行結果:🎜 🎜🎜🎜🎜🎜🎜1🎜{"タイトル":"u6d63u72b2ue785","body":"男性"}🎜🎜🎜🎜
iconv は形式を変換するときに GBK を使用しますが、GB2312 には 6763 文字しか含まれていないため、iconv はエラーを報告します。収集されていない中国語の文字が表示されると、iconv はエラーを報告します。 GBK は GB2312-80 と完全に下位互換性があり、より多くの文字をサポートしますが、それでもエラーは発生します。GB18030 は基本的に GBK と互換性があり、100 を超えるフォントを拡張します。

配列内のすべての中国語文字が欠落しているか、json_encode の後に u6d63 などが表示されます。

解決策は、urlencode() 関数を使用して以下を処理することです。json_encode の前に、urlencode() を使用してすべての配列の内容を処理し、次に json_encode() を使用してそれを JSON 文字列に変換し、最後に urldecode を使用します。 () で渡した中国語をエンコードします。

01<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><tdcourier new rgb><codecourier new important><?php01<?php

02/**************************************************************

03 *
🎜🎜🎜🎜🎜02🎜/************** ******************************************🎜🎜🎜🎜 🎜🎜 🎜🎜🎜🎜03🎜 < /code><codeCourier New',Courier,monospace! important; color:rgb(0,130,0)! important">*🎜🎜🎜 🎜

04 *  特定の関数を使用して数グループ内のすべての要素を実行します

05<codeCourier New',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">05 <tdCourier New',Courier,monospace!重要; border-width:0px 0px 0px 3px! important; border-left-style:solid! important; border-left-color:rgb( 108,226,108)!重要; フロート:なし!重要; 左:自動!重要; :auto!重要; 高さ:自動!重要; 行の高さ:1.1em!重要; 背景:なし!重要"><codeCourier,monospace!重要; min-height:継承!重要;背景:なし!重要"> *  @param string &$array 要处理的文字列

06 <codeCourier New',Courier,monospace! important; min-height:継承!重要;表示:ブロック!重要;背景:なし!重要">06 </tr></tbody>*  @param string $function 要実行関数数</table>🎜🎜🎜🎜</p><p><table><tbody><tr><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',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">07 *  @return boolean $apply_to_keys_only 否かもキー上に

08<codeCourier New',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">08 <tdCourier New',Courier,monospace!重要; border-width:0px 0px 0px 3px! important; border-left-style:solid! important; border-left-color:rgb( 108,226,108)!重要; フロート:なし!重要; 左:自動!重要; :auto!重要; 高さ:自動!重要; 行の高さ:1.1em!重要; 背景:なし!重要"><codeCourier New',Courier,monospace!重要; min-height:継承!重要;背景:なし!重要"> *  @access public

09 *09</tr></tbody><tdCourier New',Courier,monospace! important; border-width:0px 0px 0px 3px! important; border-left-style:solid! important; border-left-color:rgb( 108,226,108)!重要; フロート:なし!重要; 左:自動!重要; :auto!重要; 高さ:自動!重要; 行の高さ:1.1em!重要; 背景:なし!重要"><codeCourier New',Courier,monospace!重要; min-height:継承!重要;背景:なし!重要"> </table>🎜*🎜🎜🎜🎜🎜</p><p><table><tbody><tr><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',Courier,monospace!重要; min-height:継承!重要;表示:ブロック!重要;背景:なし!重要">10<codeCourier New',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">10 *************************************************************/

11function arrayRecursive(&$array, $function, $apply_to_keys_also<tdCourier New',Courier,monospace!重要; border-width:0px 0px 0px 3px! important; border-left-style:solid! important; border-left-color:rgb( 108,226,108)!重要; フロート:なし!重要; 左:自動!重要; :auto!重要; 高さ:自動!重要; 幅:自動!重要; min-高さ:継承!重要; ****************************************** ******************/</tr></tbody></table></p><p><table><tbody><codeCourier New',Courier,monospace! important; :block!重要; 背景:なし!重要">11<tr><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"><tdCourier New',Courier,monospace!重要;ボーダー幅:0px 0px 0px 3px!重要; border-left-style:solid!重要; border-left-color:rgb(108,226,108)!重要;アウトライン:0px!重要; float:none!重要;垂直配置:トップ!重要;位置:静的!重要;左:自動!重要;トップ:自動!重要;右:自動!重要;下:自動!重要;高さ:自動!重要;幅:自動!重要;行の高さ:1.1em!重要; min-height:継承!重要;背景:なし!重要"><codeCourier New',Courier,monospace!重要; font-weight:bold!重要; min-高さ:継承!重要; カラー:rgb(0,102,153)!重要; 背景:なし!重要"> function<codeCourier New',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">12{ arrayRecursive(&$array</tbody></table>, 🎜 <codeCourier New',Courier,monospace! important; min-height:inherit! important; background:none! important">$function🎜🎜, 🎜<codeCourier New',Courier,等幅!重要; 最小高さ:継承!重要; カラー:rgb(170,119,0)!重要; 背景:なし!重要">$apply_to_keys_only false)🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜<codeCourier New',Courier,monospace!重要; min-height:継承!重要;表示:ブロック!重要;背景:なし!重要">12🎜🎜<tdCourier New',Courier,monospace! important; border-width:0px 0px 0px 3px! important; border-left-style:solid! important; border-left-color:rgb( 108,226,108)!重要; フロート:なし!重要; 左:自動!重要; :自動!重要; 高さ:自動!重要; 行の高さ:1.1 重要; 背景:なし!重要;</p><p><table><tbody><tr><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',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">13 static $recursive_counter = 0;

14<codeCourier New',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">14 if (&#43;&#43;$recursive_counter<tdCourier New',Courier,monospace!重要; border-width:0px 0px 0px 3px! important; border-left-style:solid! important; border-left-color:rgb( 108,226,108)!重要; フロート:なし!重要; 左:自動!重要; :auto!重要; 高さ:自動!重要; 行の高さ:1.1em!重要; 背景:なし!重要"> if</tr> </tbody>(++</table><codeCourier New',Courier,monospace!重要; min-height:inherit!重要; カラー:rgb(170,119,0)!重要; 背景:なし!重要" >$recursive_counter🎜 🎜> 1000) {🎜🎜🎜🎜🎜</p><p><table><tbody><tr><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',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">15 die('可能です 深い再帰攻撃');<codeCourier New',Courier,monospace!important; min-height:inherit!important; background:none!important">);

16 }
🎜🎜🎜🎜🎜🎜16🎜🎜<tdCourier New',Courier,monospace!重要;ボーダー幅:0px 0px 0px 3px!重要; border-left-style:solid!重要; border-left-color:rgb(108,226,108)!重要;アウトライン:0px!重要; float:none!重要;垂直配置:トップ!重要;位置:静的!重要;左:自動!重要;トップ:自動!重要;右:自動!重要;下:自動!重要;高さ:自動!重要;幅:自動!重要;行の高さ:1.1em!重要; min-height:継承!重要;背景:なし!重要"><codeCourier New',Courier,monospace!重要; min-height:inherit!重要; 背景:なし!重要"> 🎜<codeCourier New',Courier,monospace!重要; min-height:継承!重要;背景:なし!重要">}🎜🎜🎜🎜🎜</p><p><table><tbody><tr><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',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">17 foreach ($array as $key => $value) {

1818        if (is_array($valueif (is_array
🎜(🎜$value🎜🎜)) {🎜🎜🎜🎜🎜

1919            arrayRecursive($array[$key], $function$apply_to_keys_also);

20 arrayRecursive($array🎜[$key🎜]、$function🎜, $apply_to_keys_も🎜);🎜
🎜🎜🎜🎜🎜🎜20🎜🎜🎜 &nbs
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:PHP の「ピット」次の記事:PHP の「ピット」