{"id":47399,"date":"2024-12-02T16:42:49","date_gmt":"2024-12-02T08:42:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47399\/"},"modified":"2024-12-02T16:42:49","modified_gmt":"2024-12-02T08:42:49","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%b8%8e-json-%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47399\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u4e0e JSON \u4ea4\u4e92"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u4e0e JSON \u4ea4\u4e92<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e JSON \u4ea4\u4e92\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p>PHP \u51fd\u6570\u5728\u5904\u7406 JSON \u6570\u636e\u65f6\uff0c\u63d0\u4f9b\u4ee5\u4e0b\u529f\u80fd\uff1ajson_encode() \u5c06 PHP \u53d8\u91cf\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002json_decode() \u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a PHP \u53d8\u91cf\u3002\u5177\u4f53\u8bed\u6cd5\u548c\u5b9e\u6218\u6848\u4f8b\u89c1\u6587\u7ae0\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729915772671c6b7c35c32.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e JSON \u4ea4\u4e92\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e JSON \u4ea4\u4e92\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5982\u4f55\u4e0e JSON \u4ea4\u4e92<\/strong><\/p>\n<p>JSON \u662f\u4e00\u79cd\u5e7f\u6cdb\u7528\u4e8e\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f20\u8f93\u548c\u63a5\u6536\u6570\u636e\u7684\u8f7b\u91cf\u7ea7\u6570\u636e\u683c\u5f0f\u3002PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u5904\u7406 JSON \u6570\u636e\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>json_encode()\uff1a<\/strong> \u5c06 PHP \u53d8\u91cf\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/li>\n<li><strong>json_decode()\uff1a<\/strong> \u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a PHP \u53d8\u91cf\u3002<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>\/\/ \u7f16\u7801 PHP \u53d8\u91cf\u5230 JSON \u5b57\u7b26\u4e32\n$jsonStr = json_encode($phpVar);\n\n\/\/ \u89e3\u7801 JSON \u5b57\u7b26\u4e32\u5230 PHP \u53d8\u91cf\n$phpVar = json_decode($jsonStr);<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p><strong>\u5c06 PHP \u6570\u7ec4\u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<pre>$phpArray = array(\"name\" =&gt; \"John Doe\", \"age\" =&gt; 30, \"city\" =&gt; \"New York\");\n$jsonStr = json_encode($phpArray);<\/pre>\n<p><strong>\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u7801\u4e3a PHP \u6570\u7ec4\uff1a<\/strong><\/p>\n<pre>$jsonStr = '{\"name\": \"John Doe\", \"age\": 30, \"city\": \"New York\"}';\n$phpArray = json_decode($jsonStr, true); \/\/ \u5c06 `true` \u8bbe\u7f6e\u4e3a `true` \u4ee5\u83b7\u53d6\u5173\u8054\u6570\u7ec4\n\n\/\/ \u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\necho $phpArray[\"name\"]; \/\/ \u8f93\u51fa\uff1aJohn Doe<\/pre>\n<p><strong>\u5176\u4ed6\u6709\u7528\u7684 PHP JSON \u51fd\u6570\uff1a<\/strong><\/p>\n<ul>\n<li><strong>json_last_error()\uff1a<\/strong> \u8fd4\u56de\u4e0a\u6b21 JSON \u64cd\u4f5c\u4e2d\u53d1\u751f\u7684\u9519\u8bef\u4ee3\u7801\u3002<\/li>\n<li><strong>json_last_error_msg()\uff1a<\/strong> \u8fd4\u56de\u4e0a\u4e00\u4e2a JSON \u9519\u8bef\u7684\u63cf\u8ff0\u6027\u6d88\u606f\u3002<\/li>\n<li><strong>json_encode_options()\uff1a<\/strong> \u8bbe\u7f6e\u5982\u4f55\u5c06 PHP \u53d8\u91cf\u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\u7684\u9009\u9879\u3002<\/li>\n<li><strong>json_decode_options()\uff1a<\/strong> \u8bbe\u7f6e\u5982\u4f55\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u7801\u4e3a PHP \u53d8\u91cf\u7684\u9009\u9879\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u4e0e JSON \u4ea4\u4e92 \u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e JSON \u4ea4\u4e92\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 PHP \u51fd\u6570\u5728\u5904\u7406 JSON \u6570\u636e\u65f6\uff0c\u63d0\u4f9b\u4ee5\u4e0b\u529f\u80fd\uff1ajson_encode() \u5c06 PHP \u53d8\u91cf\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002json_decode() \u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a PHP \u53d8\u91cf\u3002\u5177\u4f53\u8bed\u6cd5\u548c\u5b9e\u6218\u6848\u4f8b\u89c1\u6587\u7ae0\u3002 PHP \u51fd\u6570\u5982\u4f55\u4e0e JSON \u4ea4\u4e92 JSON \u662f\u4e00\u79cd\u5e7f\u6cdb\u7528\u4e8e\u5728 Web \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f20\u8f93\u548c\u63a5\u6536\u6570\u636e\u7684\u8f7b\u91cf\u7ea7\u6570\u636e\u683c\u5f0f\u3002PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u5904\u7406 JSON \u6570\u636e\uff0c\u5305\u62ec\uff1a json_encode()\uff1a \u5c06 PHP \u53d8\u91cf\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002 json_decode()\uff1a \u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a PHP \u53d8\u91cf\u3002 \u8bed\u6cd5\uff1a \/\/ \u7f16\u7801 PHP \u53d8\u91cf\u5230 JSON \u5b57\u7b26\u4e32 $jsonStr = json_encode($phpVar); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47399","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47399"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47399\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}