PHP を使用した JSON の解析
JSON は、Web アプリケーションと API の間でデータを交換するためによく使用される一般的なデータ形式です。 PHP では、json_decode() 関數(shù)を使用して、JSON 文字列を PHP 配列またはオブジェクトに解析できます。
JSON をオブジェクトとして解析するには、$json = json_decode($data) 構(gòu)文を使用するだけです。結(jié)果のオブジェクトには、ドット表記を使用してアクセスできます。
ただし、JSON を配列として解析する場(chǎng)合は、2 番目の引數(shù) true を json_decode() 関數(shù)に渡す必要があります。これにより、キーがプロパティ名、値が対応する値である連想配列が返されます。
例として次の JSON データを考えてみましょう:
{ "kind": "shopping#products", "etag": "\"T9uPnY2MZMB71TDpKXXZdr3yWX4/qtJ5vmpftFWNfijyLD9ti2Xpj-w\"", "id": "tag:google.com,2010:shopping/products", "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&q\u003dsony&maxResults\u003d3&startIndex\u003d2", "nextLink": "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&q\u003dsony&maxResults\u003d3&startIndex\u003d5", "previousLink": "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&q\u003dsony&maxResults\u003d3&startIndex\u003d1", "totalItems": 633694, "startIndex": 2, "itemsPerPage": 3, "currentItemCount": 3, "items": [ { "kind": "shopping#product", "id": "tag:google.com,2010:shopping/products/1161353/11882813508247586172", "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/1161353/gid/11882813508247586172", "product": { "googleId": "11882813508247586172", "author": { "name": "Buy.com", "accountId": "1161353" }, "creationTime": "2011-04-24T05:13:38.000Z", "modificationTime": "2011-08-05T17:45:24.000Z", "country": "US", "language": "en", "title": "Sony BRAVIA KDL-46EX720 46 inch 3D LED HDTV 1080p 120Hz", "description": "Entertainment lovers the slim Sony EX720-Series LED HDTV is for you. See precise motion detail plus watch your favorite 2D or 3D entertainment in clear, vivid Full HD 1080p picture quality with incredible contrast. You can even connect to the internet and access a great selection of online entertainment with Netflix , Hulu Plus , Pandora , Qriocity and more - there s always something on. Plus video chat with friends and family when you Skype on the big screen.", "link": "http://clickfrom.buy.com/default.asp?adid\u003d17902&sURL\u003dhttp%3A%2F%2Fwww.buy.com%2Fprod%2Fsony-bravia-kdl-46ex720-46-3d-led-hdtv-1080p-120hz%2Fq%2Fsellerid%2F10004001%2Floc%2F111%2F219891693.html", "brand": "Sony", "condition": "new", "gtin": "00027242817081", "gtins": [ "00027242817081" ], "inventories": [ { "channel": "online", "availability": "inStock", "price": 1348.0, "currency": "USD" } ], "images": [ { "link": "http://ak.buy.com/PI/0/1000/219891693.jpg" } ] } } ] }
この JSON を次のように解析するには配列の場(chǎng)合は、次の構(gòu)文を使用します:
<code class="php">$json = json_decode($data, true);</code>
これにより、次の配列が返されます:
[ "kind" => "shopping#products", "etag" => "\"T9uPnY2MZMB71TDpKXXZdr3yWX4/qtJ5vmpftFWNfijyLD9ti2Xpj-w\"", "id" => "tag:google.com,2010:shopping/products", "selfLink" => "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&q\u003dsony&maxResults\u003d3&startIndex\u003d2", "nextLink" => "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&q\u003dsony&maxResults\u003d3&startIndex\u003d5", "previousLink" => "https://www.googleapis.com/shopping/search/v1/public/products?country\u003dus&q\u003dsony&maxResults\u003d3&startIndex\u003d1", "totalItems" => 633694, "startIndex" => 2, "itemsPerPage" => 3, "currentItemCount" => 3, "items" => [ [ "kind" => "shopping#product", "id" => "tag:google.com,2010:shopping/products/1161353/11882813508247586172", "selfLink" => "https://www.googleapis.com/shopping/search/v1/public/products/1161353/gid/11882813508247586172", "product" => [ "googleId" => "11882813508247586172", "author" => [ "name" => "Buy.com", "accountId" => "1161353" ], "creationTime" => "2011-04-24T05:13:38.000Z", "modificationTime" => "2011-08-05T17:45:24.000Z", "country" => "US", "language" => "en", "title" => "Sony BRAVIA KDL-46EX720 46 inch 3D LED HDTV 1080p 120Hz", "description" => "Entertainment lovers the slim Sony EX720-Series LED HDTV is for you. See precise motion detail plus watch your favorite 2D or 3D entertainment in clear, vivid Full HD 1080p picture quality with incredible contrast. You can even connect to the internet and access a great selection of online entertainment with Netflix , Hulu Plus , Pandora , Qriocity and more - there s always something on. Plus video chat with friends and family when you Skype on the big screen.",
以上がPHP で JSON を配列またはオブジェクトとして解析する方法の詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國(guó)語(yǔ) Web サイトの他の関連記事を參照してください。

ホットAIツール

Undress AI Tool
脫衣畫(huà)像を無(wú)料で

Undresser.AI Undress
リアルなヌード寫(xiě)真を作成する AI 搭載アプリ

AI Clothes Remover
寫(xiě)真から衣服を削除するオンライン AI ツール。

Clothoff.io
AI衣類(lèi)リムーバー

Video Face Swap
完全無(wú)料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡(jiǎn)単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無(wú)料のコードエディター

SublimeText3 中國(guó)語(yǔ)版
中國(guó)語(yǔ)版、とても使いやすい

ゼンドスタジオ 13.0.1
強(qiáng)力な PHP 統(tǒng)合開(kāi)発環(huán)境

ドリームウィーバー CS6
ビジュアル Web 開(kāi)発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

PHP変數(shù)スコープの一般的な問(wèn)題とソリューションには次のものが含まれます。1。グローバル変數(shù)は関數(shù)內(nèi)でアクセスできず、グローバルキーワードまたはパラメーターを使用して渡す必要があります。 2。靜的変數(shù)は靜的で宣言され、1回のみ初期化され、値は複數(shù)の呼び出し間で維持されます。 3. $ _GETや$ _POSTなどのハイパーグローバル変數(shù)は、任意の範(fàn)囲で直接使用できますが、安全なフィルタリングに注意を払う必要があります。 4.匿名関數(shù)は、使用キーワードを使用して親スコープ変數(shù)を?qū)毪工氡匾ⅳ?、外部変?shù)を変更する場(chǎng)合は、參照を渡す必要があります。これらのルールを習(xí)得すると、エラーを回避し、コードの安定性が向上するのに役立ちます。

PHPファイルのアップロードを安全に処理するには、ソースとタイプを確認(rèn)し、ファイル名とパスを制御し、サーバー制限を設(shè)定し、メディアファイルを2回プロセスする必要があります。 1.トークンを介してCSRFを防ぐためにアップロードソースを確認(rèn)し、ホワイトリストコントロールを使用してFINFO_FILEを介して実際のMIMEタイプを検出します。 2。ファイルをランダムな文字列に変更し、検出タイプに従って非WEBディレクトリに保存する拡張機(jī)能を決定します。 3。PHP構(gòu)成は、アップロードサイズを制限し、一時(shí)的なディレクトリnginx/apacheはアップロードディレクトリへのアクセスを禁止します。 4. GDライブラリは寫(xiě)真を再利用して、潛在的な悪意のあるデータをクリアします。

PHPコメントコードには3つの一般的な方法があります。1。//#を使用して1行のコードをブロックすると、//を使用することをお?jiǎng)幛幛筏蓼埂?2。使用/.../複數(shù)の行でコードブロックをラップするには、ネストすることはできませんが交差することができます。 3. / if(){}を使用するなどの組み合わせスキルコメントロジックブロックを制御するか、エディターショートカットキーで効率を改善するには、シンボルを閉じることに注意を払い、使用時(shí)にネストを避ける必要があります。

ageneratorinphpisamemory-efficientwaytoateate-overdeatatasetasetasetasetsinging valueseintimeintimeturningthemallatonce.1.generatorsususedeywordproducevaluesedemand、memoryusage.2を還元すること。2

PHPコメントを書(shū)くための鍵は、目的と仕様を明確にすることです。コメントは、「何が行われたのか」ではなく「なぜ」を説明する必要があり、冗長(zhǎng)性や単純さを避けてください。 1.読みやすさとツールの互換性を向上させるために、クラスおよびメソッドの説明にdocblock(/*/)などの統(tǒng)合形式を使用します。 2。JSジャンプを手動(dòng)で出力する必要がある理由など、ロジックの背後にある理由を強(qiáng)調(diào)します。 3.複雑なコードの前に概要説明を追加し、手順でプロセスを説明し、全體的なアイデアを理解するのに役立ちます。 4. TodoとFixmeを合理的に使用して、To Doアイテムと問(wèn)題をマークして、その後の追跡とコラボレーションを促進(jìn)します。優(yōu)れた注釈は、通信コストを削減し、コードメンテナンスの効率を向上させることができます。

PHPでは、四角い括弧または巻き毛裝具を使用して文字列固有のインデックス文字を取得できますが、正方形のブラケットをお?jiǎng)幛幛筏蓼?。インデックス?から始まり、範(fàn)囲外のアクセスはnull値を返し、値を割り當(dāng)てることができません。 MB_SUBSTRは、マルチバイト文字を処理するために必要です。例:$ str = "hello"; echo $ str [0];出力h; MB_Substr($ str、1,1)などの漢字は、正しい結(jié)果を得る必要があります。実際のアプリケーションでは、ループする前に文字列の長(zhǎng)さをチェックする必要があり、ダイナミック文字列を有効性のために検証する必要があり、多言語(yǔ)プロジェクトはマルチバイトセキュリティ関數(shù)を均一に使用することをお?jiǎng)幛幛筏蓼埂?/p>

to installphpquickly、usexampponwindowsorhomebrewonmacos.1.onwindows、downloadandinstallxampp、selectcomponents、startapache、andplacefilesinhtdocs.2

tolearnphpefctivially、startbysettingupalocalserverenvironmentusingtoolslikexamppandacodeeditorlikevscode.1)instalxamppforapa Che、mysql、andphp.2)useocodeeditorforsyntaxsupport.3)testyoursetup withasimplephpfile.next、Learnpbasicsincludingvariables、ech
