{"id":12318,"date":"2020-02-09T19:37:14","date_gmt":"2020-02-10T00:37:14","guid":{"rendered":"httpss:\/\/www.powenko.com\/wordpress\/?p=12318"},"modified":"2020-02-09T19:37:14","modified_gmt":"2020-02-10T00:37:14","slug":"02-php-post-all-data-save-to-json-string","status":"publish","type":"post","link":"https:\/\/www.powenko.com\/wordpress\/?p=12318","title":{"rendered":"02 PHP POST  all data,  Save to JSON string"},"content":{"rendered":"<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/*\r\nforeach (array_keys($_POST) as $field)\r\n{\r\necho $_POST&#x5B;$field];\r\n}\r\n*\/\r\n$jsonString=&quot;{&quot;;\r\n$i=0;\r\nforeach ($_POST as $name =&gt; $val)\r\n{\r\n\ttry {\r\n\t\tif($i&gt;0){$jsonString=$jsonString.&quot;,&quot;;}\r\n\t\t$i=$i+1;\r\n\t\tif ( is_array( $val ) == true ) {\r\n\t\t\t$jsonString = $jsonString.&quot;\\&quot;&quot; . $name . &quot;\\&quot;:&quot; . json_encode($val) . &quot;&quot;;\r\n\t\t} else {\r\n\t\t\tif(is_numeric($val)){\r\n\t\t\t\t$jsonString = $jsonString.&quot;\\&quot;&quot; . $name . &quot;\\&quot;:&quot; . $val . &quot;&quot;;\r\n\t\t\t}else{\r\n\t\t\t\t$jsonString = $jsonString.&quot;\\&quot;&quot; . $name . &quot;\\&quot;:\\&quot;&quot; . $val . &quot;\\&quot;&quot;;\r\n\t\t\t}\r\n\t\t\t\/\/ echo htmlspecialchars( $name . ': ' . $val ) . &quot;&lt;br&gt;\\n&quot;;\r\n\t\t}\r\n\t}catch  (Exception $e) {\r\n\t}\r\n}\r\n$jsonString=$jsonString.&quot;}&quot;;\r\necho $jsonString;\r\n\r\n\r\n \r\n?&gt;\r\n<\/pre>\n<p>output:<\/p>\n<p>{&#8220;aaa&#8221;:&#8221;123@gmail.com&#8221;,&#8221;bbb&#8221;:&#8221;asds&#8221;,&#8221;cccc&#8221;:&#8221;&#8221;,&#8221;dddd&#8221;:[&#8220;cccc&#8221;]}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/* foreach (array_keys($_POST) as $field) { ec [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[566],"tags":[],"class_list":["post-12318","post","type-post","status-publish","format-standard","hentry","category-ch05-json"],"_links":{"self":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/12318"}],"collection":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12318"}],"version-history":[{"count":2,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/12318\/revisions"}],"predecessor-version":[{"id":12320,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/12318\/revisions\/12320"}],"wp:attachment":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}