
{"id":7578,"date":"2023-07-26T04:46:41","date_gmt":"2023-07-25T21:46:41","guid":{"rendered":"https:\/\/tapchicntt.com\/?p=7578"},"modified":"2023-07-28T11:37:59","modified_gmt":"2023-07-28T04:37:59","slug":"php-huong-dan-doc-va-luu-file-json-bang-php","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/php-huong-dan-doc-va-luu-file-json-bang-php\/","title":{"rendered":"[PHP] H\u01b0\u1edbng d\u1eabn \u0111\u1ecdc v\u00e0 l\u01b0u file json b\u1eb1ng php"},"content":{"rendered":"\n<p>Ch\u00e0o c\u00e1c b\u1ea1n, trong b\u00e0i h\u01b0\u1edbng d\u1eabn n\u00e0y ch\u00fang t\u00f4i s\u1ebd h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n th\u1ef1c hi\u1ec7n \u0111\u1ecdc v\u00e0 l\u01b0u file json<\/p>\n\n\n\n<p>D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t h\u00e0m PHP \u0111\u1ec3 \u0111\u1ecdc file JSON:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nfunction read_json_file($file_path) {\n  $data = file_get_contents($file_path);\n  $json_data = json_decode($data, true);\n  return $json_data;\n}\n<\/pre><\/div>\n\n\n<p>H\u00e0m n\u00e0y s\u1ebd nh\u1eadn \u0111\u01b0\u1eddng d\u1eabn \u0111\u1ebfn file JSON l\u00e0m \u0111\u1ea7u v\u00e0o v\u00e0 tr\u1ea3 v\u1ec1 d\u1eef li\u1ec7u JSON trong file d\u01b0\u1edbi d\u1ea1ng m\u1ed9t m\u1ea3ng PHP.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$json_data = read_json_file(&#039;data.json&#039;);\n<\/pre><\/div>\n\n\n<p>H\u00e0m n\u00e0y s\u1ebd \u0111\u1ecdc file <code>data.json<\/code> v\u00e0 tr\u1ea3 v\u1ec1 d\u1eef li\u1ec7u JSON trong file d\u01b0\u1edbi d\u1ea1ng m\u1ed9t m\u1ea3ng PHP. B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng m\u1ea3ng n\u00e0y \u0111\u1ec3 truy c\u1eadp c\u00e1c kh\u00f3a v\u00e0 gi\u00e1 tr\u1ecb trong file JSON.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\necho $json_data&#x5B;&#039;name&#039;]; \/\/ T\u00ean c\u1ee7a kh\u00f3a `name`\necho $json_data&#x5B;&#039;age&#039;]; \/\/ Tu\u1ed5i c\u1ee7a kh\u00f3a `age`\n<\/pre><\/div>\n\n\n<p>D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t h\u00e0m PHP \u0111\u1ec3 l\u01b0u d\u1eef li\u1ec7u JSON v\u00e0o file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nfunction save_json_file($file_path, $data) {\n  $json_string = json_encode($data, JSON_PRETTY_PRINT);\n  file_put_contents($file_path, $json_string);\n}\n<\/pre><\/div>\n\n\n<p>H\u00e0m n\u00e0y s\u1ebd nh\u1eadn \u0111\u01b0\u1eddng d\u1eabn \u0111\u1ebfn file JSON v\u00e0 d\u1eef li\u1ec7u JSON l\u00e0m \u0111\u1ea7u v\u00e0o v\u00e0 l\u01b0u d\u1eef li\u1ec7u JSON v\u00e0o file.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$data = &#x5B;&#039;name&#039; =&gt; &#039;John Doe&#039;, &#039;age&#039; =&gt; 30];\nsave_json_file(&#039;data.json&#039;, $data);\n<\/pre><\/div>\n\n\n<p>H\u00e0m n\u00e0y s\u1ebd l\u01b0u d\u1eef li\u1ec7u JSON trong m\u1ea3ng <code>data<\/code> v\u00e0o file <code>data.json<\/code>. B\u1ea1n c\u00f3 th\u1ec3 m\u1edf file <code>data.json<\/code> trong tr\u00ecnh so\u1ea1n th\u1ea3o v\u0103n b\u1ea3n \u0111\u1ec3 xem d\u1eef li\u1ec7u JSON \u0111\u00e3 \u0111\u01b0\u1ee3c l\u01b0u th\u00e0nh c\u00f4ng.<\/p>\n\n\n\n<p>\u0110\u00e2y ch\u1ec9 l\u00e0 m\u1ed9t v\u00ed d\u1ee5 v\u1ec1 c\u00e1ch l\u01b0u d\u1eef li\u1ec7u JSON v\u00e0o file b\u1eb1ng PHP. B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng h\u00e0m n\u00e0y \u0111\u1ec3 l\u01b0u b\u1ea5t k\u1ef3 d\u1eef li\u1ec7u JSON n\u00e0o v\u00e0o file.<\/p>\n\n\n\n<p>D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t class PHP c\u00f3 ch\u1ee9a c\u1ea3 hai h\u00e0m <code>read_json_file()<\/code> v\u00e0 <code>save_json_file()<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nclass JsonFile {\n  public function read_json_file($file_path) {\n    $data = file_get_contents($file_path);\n    $json_data = json_decode($data, true);\n    return $json_data;\n  }\n\n  public function save_json_file($file_path, $data) {\n    $json_string = json_encode($data, JSON_PRETTY_PRINT);\n    file_put_contents($file_path, $json_string);\n  }\n}\n<\/pre><\/div>\n\n\n<p>B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng class n\u00e0y \u0111\u1ec3 \u0111\u1ecdc v\u00e0 l\u01b0u d\u1eef li\u1ec7u JSON v\u00e0o file b\u1eb1ng c\u00e1ch t\u1ea1o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a class v\u00e0 sau \u0111\u00f3 g\u1ecdi c\u00e1c ph\u01b0\u01a1ng th\u1ee9c <code>read_json_file()<\/code> v\u00e0 <code>save_json_file()<\/code>. V\u00ed d\u1ee5:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$json_file = new JsonFile();\n$json_data = $json_file-&gt;read_json_file(&#039;data.json&#039;);\n$json_file-&gt;save_json_file(&#039;data.json&#039;, $json_data);\n<\/pre><\/div>\n\n\n<p>\u0110o\u1ea1n m\u00e3 n\u00e0y s\u1ebd \u0111\u1ecdc file <code>data.json<\/code>, gi\u1ea3i m\u00e3 d\u1eef li\u1ec7u JSON trong file th\u00e0nh m\u1ed9t m\u1ea3ng PHP v\u00e0 sau \u0111\u00f3 l\u01b0u d\u1eef li\u1ec7u JSON trong m\u1ea3ng \u0111\u00f3 tr\u1edf l\u1ea1i file <code>data.json<\/code>.<\/p>\n\n\n\n<p>Ch\u00fac c\u00e1c b\u1ea1n th\u00e0nh c\u00f4ng!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ch\u00e0o c\u00e1c b\u1ea1n, trong b\u00e0i h\u01b0\u1edbng d\u1eabn n\u00e0y ch\u00fang t\u00f4i s\u1ebd h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n th\u1ef1c hi\u1ec7n \u0111\u1ecdc v\u00e0 l\u01b0u [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":7580,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[212],"tags":[],"class_list":["post-7578","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-thu-thuat"],"views":843,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/7578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/comments?post=7578"}],"version-history":[{"count":5,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/7578\/revisions"}],"predecessor-version":[{"id":7741,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/7578\/revisions\/7741"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media\/7580"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=7578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=7578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=7578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}