検索

php json_encode

Aug 08, 2016 am 09:27 AM
arrayencodejsonnbspquot

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><?php 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

02<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>$arr <codecourier new min-height:inherit background:none>= <codecourier new font-weight:bold min-height:inherit color:rgb background:none>array</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>03<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 background:none>(</codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>🎜🎜🎜🎜🎜<codecourier new important min-height:inherit display:block background:none>02🎜<tdcourier new important border-width:0px border-left-style:solid border-left-color:rgb><codecourier min-height:inherit important color:rgb background:none>$arr <codecourier new important min-height:inherit>= <codecourier new important font-weight:bold min-height:inherit color:rgb>配列🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜<codecourier new important>03🎜<tdcourier new important border-width:0px border-left-style:solid border-left-color:rgb :auto><codecourier new> <codecourier new>(コード>コード>🎜🎜🎜🎜<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:>04<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></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>05<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>'Age'<codecourier new min-height:inherit background:none>=>22</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>06<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 background:none>);<tdcourier new important border-width:0px border-left-style:solid border-left-color :rgb min-height: background:none> <codecourier new important min-height:inherit color:blue background:none>'Name' <codecourier new min-height:>=><codecourier new min-height:inherit>'你妹' <codecourier new min-height:>,</codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>🎜🎜🎜🎜🎜🎜<codecourier new min-height:inherit important>05🎜<tdcourier new important border-left-style:solid border-left-color:rgb float:none min-height:><codecourier new min-height:inherit> <codecourier new min-height:>'年齢'<codecourier new min-height:inherit>=>22🎜🎜 🎜🎜🎜🎜🎜🎜🎜🎜<codecourier new min-height:>06🎜<tdcourier new important border-width:0px border-left-style:solid border-left-color :rgb min-height:> <codecourier new min-height:inherit>);🎜🎜🎜🎜<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:>07<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></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>08<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>$jsonencode<codecourier new min-height:inherit background:none><tdcourier new border-width:0px important border-left-style:solid border-left-color:rgb :auto> <codecourier new min-height:>08<codecourier new min-height:inherit color:rgb background:none>$arr<codecourier new min-height:inherit background:none>);</codecourier></codecourier></codecourier></tdcourier></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>09<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>echo <codecourier new min-height:inherit color:rgb background:none>$jsonencode<codecourier new min-height:inherit background:none>;<tdcourier new border-width:0px important border-left-style:solid border-left-color:rgb :auto><codecourier new min-height:>$jsonencode</codecourier></tdcourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr> </tbody>= json_encode(</table> <codecourier new important min-height:inherit color:rgb background:none>$arr🎜🎜);🎜🎜🎜🎜 🎜🎜🎜🎜🎜🎜<codecourier new important min-height:inherit display:block important:none>09🎜🎜<tdcourier new border-left-style:solid border-left-color:rgb float:none min-height: background:none important><codecourier new important min-height:inherit color:rgb background:none>echo🎜 <codecourier new min-height:>$jsonencode🎜🎜;🎜🎜🎜🎜🎜<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:>10<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></codecourier></tdcourier></tr></tbody></table> <p>程序运行结果如下:</p> <divcourier new min-height:inherit><p></p> <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>1<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>:null,<codecourier new min-height:inherit color:blue background:none>"Age"<codecourier new min-height:inherit background:none>:20}</codecourier></codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p>json_encode 函数中中文被编码成 null 了,Google 了一下,很简单,为了与前端紧密结合,Json 只支持 utf-8 编码,我认为是前端的 Javascript 也是 utf-8 的原因。</p> <divcourier new min-height:inherit><p></p> <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>1<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 ?>
🎜🎜🎜🎜程序运行结果如下:🎜🎜🎜 🎜🎜🎜🎜1🎜<tdcourier new important border-width:0px border-left-style:solid border-left-color :rgb min-height:>{<codecourier new min->"名前"<codecourier new min-height: background:none important>:null,<codecourier new important min-height:inherit color:blue background:none>"Age"<codecourier new min-height: background:none important>:20}🎜🎜🎜🎜🎜🎜🎜🎜json_encode関数数中中文被编码成 null 了,Google 了一下,很简单,為了与前端紧密结合,Json のみ utf- をサポート8 暗号、フロントエンドの Javascript も utf-8 の原因であると考えています。等幅!重要; min-height:継承!重要;表示:ブロック!重要;背景:なし!重要">1🎜<tdcourier new important border-width:0px border-left-style:solid border-left-color :rgb min-height:><?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><?php 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

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

03 *
🎜🎜🎜🎜🎜02🎜/************** ******************************************🎜🎜🎜🎜 🎜🎜 🎜🎜🎜🎜03🎜 *🎜🎜🎜 🎜

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

0505  *  @param string &$array 要处理的文字列

06 06 *  @param string $function 要実行関数数
🎜🎜🎜🎜

07 *  @return boolean $apply_to_keys_only 否かもキー上に

0808  *  @access public

09 *09
🎜*🎜🎜🎜🎜🎜

1010 *************************************************************/

11function arrayRecursive(&$array$function$apply_to_keys_also ****************************************** ******************/

11 function12{ arrayRecursive(&$array
, 🎜 $function🎜🎜, 🎜$apply_to_keys_only false)🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜12🎜🎜

13    static $recursive_counter = 0;

1414    if (++$recursive_counter if(++
$recursive_counter🎜 🎜> 1000) {🎜🎜🎜🎜🎜

15        die('可能です 深い再帰攻撃'););

16    }
🎜🎜🎜🎜🎜🎜16🎜🎜 🎜}🎜🎜🎜🎜🎜

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パフォーマンスチューニングMay 14, 2025 am 12:13 AM

thesecrettokeepingaphp-poweredwebsterunningsmootlyunderheavyloadinvolvesseveralkeystrategies:1)emform opcodecoduceSciptionexecutiontime、2)aatabasequerycachingwithiThing withiThistolessendavasoload、

PHPでの依存関係注射:初心者向けのコード例PHPでの依存関係注射:初心者向けのコード例May 14, 2025 am 12:08 AM

コードをより明確かつ維持しやすくするため、依存関係が関心(DI)に注意する必要があります。 1)DIは、クラスを切り離すことにより、よりモジュール化されます。2)テストとコードの柔軟性の利便性を向上させ、3)DIコンテナを使用して複雑な依存関係を管理しますが、パフォーマンスの影響と円形の依存関係に注意してください。

PHPパフォーマンス:アプリケーションを最適化することは可能ですか?PHPパフォーマンス:アプリケーションを最適化することは可能ですか?May 14, 2025 am 12:04 AM

はい、最適化されたAphPossibleandessention.1)CachingingusapCutoredatedAtabaseload.2)最適化、効率的なQueries、およびConnectionPooling.3)EnhcodeCodewithBultinctions、Avoididingglobalbariables、およびUsingopcodeching

PHPパフォーマンスの最適化:究極のガイドPHPパフォーマンスの最適化:究極のガイドMay 14, 2025 am 12:02 AM

keyStrategIestsoSificlyvoostphpappliceperformanceare:1)useopcodecachinglikeToreexecutiontime、2)最適化abaseの相互作用とプロペラインデックス、3)3)構成

PHP依存性噴射コンテナ:クイックスタートPHP依存性噴射コンテナ:クイックスタートMay 13, 2025 am 12:11 AM

aphpDependencyInjectionContaineriSATOULTAINATINAGECLASSDEPTINCIES、強化測定性、テスト可能性、および維持可能性。

PHPの依存噴射対サービスロケーターPHPの依存噴射対サービスロケーターMay 13, 2025 am 12:10 AM

SELECT DEPENTENCINGINOFCENT(DI)大規模なアプリケーションの場合、ServicElocatorは小さなプロジェクトまたはプロトタイプに適しています。 1)DIは、コンストラクターインジェクションを通じてコードのテスト可能性とモジュール性を改善します。 2)ServiceLocatorは、センター登録を通じてサービスを取得します。これは便利ですが、コードカップリングの増加につながる可能性があります。

PHPパフォーマンス最適化戦略。PHPパフォーマンス最適化戦略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedforspeedandEfficiencyby:1)enabingopcacheinphp.ini、2)PreparedStatementswithpordatabasequeriesを使用して、3)LoopswithArray_filterandarray_mapfordataprocessing、4)の構成ngincasaSearverseproxy、5)

PHPメールの検証:電子メールが正しく送信されるようにしますPHPメールの検証:電子メールが正しく送信されるようにしますMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター