1. What is JSON
JSON is a data storage format used to communicate the interaction between client-side Javascript and server-side PHP. We pass the string generated by JSON using PHP to the front-end Javascript, and Javascirpt can easily decode it into JSON and then apply it.
2. How to use JSON
PHP can use two functions, json_encode() and json_decode(), to operate JSON - one for encoding and one for decoding. json_encode() can convert an array into text data in json format for easy storage and reading, while json_decode() can directly convert json data into an array for easy calling.
<code class="hljs vbscript-html"><span class="xml"><span class="php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-variable">$arr</span> = <span class="hljs-keyword">array</span>( <span class="hljs-string">'name'</span> =<span class="hljs-string">'刘璐'</span>, <span class="hljs-string">'nick'</span> =<span class="hljs-string">'璐小璐'</span>, <span class="hljs-string">'age'</span> =<span class="hljs-string">'26'</span>,`` <span class="hljs-string">'contact'</span> =<span class="hljs-keyword">array</span>( <span class="hljs-string">'phone'</span> =<span class="hljs-string">'13718136109'</span>, <span class="hljs-string">'address'</span> =<span class="hljs-string">'Beijing ifdoo'</span> ) ); <span class="hljs-variable">$str</span> = json_encode(<span class="hljs-variable">$arr</span>); <span class="hljs-keyword">echo</span> <span class="hljs-string">"getProfile($str)"</span>; <span class="hljs-preprocessor">?></span></span> </span></code>
The result is:
{"name":"u5218u7490","nick":"u7490u5c0fu7490","age":"26","contact":{"phone":"13718136109","address":"u5317u4eac u5f97u8c46"}}
3. JSON format data and WEB front-end JS complete asynchronous interaction process
After PHP uses json_encode() to convert the array into json format data, this json string is equivalent to an array in JavaScript. After assigning it to a variable, you can operate on the array.
<code class="hljs haskell"><script <span class="hljs-typedef"><span class="hljs-keyword">type</span>="text/javascript></span> var arr = {<span class="hljs-string">"name"</span>:<span class="hljs-string">"\u5218\u7490"</span>,<span class="hljs-string">"nick"</span>:<span class="hljs-string">"\u7490\u5c0f\u7490"</span>,<span class="hljs-string">"age"</span>:<span class="hljs-string">"26"</span>,<span class="hljs-string">"contact"</span>:{<span class="hljs-string">"phone"</span>:<span class="hljs-string">"13718136109"</span>,<span class="hljs-string">"address"</span>:<span class="hljs-string">"\u5317\u4eac \u5f97\u8c46"</span>}}; alert(arr.name); </script> </code>
4. Example
index.html
<code class="hljs vbscript-html"><span class="xml"><span class="hljs-tag"><<span class="hljs-title">html</span>></span> <span class="hljs-tag"><<span class="hljs-title">head</span>></span> <span class="hljs-tag"><<span class="hljs-title">title</span>></span>json demo<span class="hljs-tag"></<span class="hljs-title">title</span>></span> <span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/javascript"</span>></span><span class="javascript"> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getProfile</span><span class="hljs-params">(str)</span> </span>{ <span class="hljs-keyword">var</span> arr = str; <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'nick'</span>).innerHTML = arr.nick; } </span><span class="hljs-tag"></<span class="hljs-title">script</span>></span> <span class="hljs-tag"></<span class="hljs-title">head</span>></span> <span class="hljs-tag"><<span class="hljs-title">body</span>></span> <span class="hljs-tag"><<span class="hljs-title">div</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"nick"</span>></span><span class="hljs-tag"></<span class="hljs-title">div</span>></span> <span class="hljs-tag"></<span class="hljs-title">body</span>></span> <span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text/javascript"</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"./profile.php"</span>></span><span class="hljs-tag"></<span class="hljs-title">script</span>></span> <span class="hljs-tag"></<span class="hljs-title">html</span>></span> </span></code>
profile.php
<code class="hljs vbscript-html"><span class="xml"><span class="php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-variable">$arr</span> = <span class="hljs-keyword">array</span>( <span class="hljs-string">'name'</span> => <span class="hljs-string">'刘璐'</span>, <span class="hljs-string">'nick'</span> => <span class="hljs-string">'璐小璐'</span>, <span class="hljs-string">'age'</span> => <span class="hljs-string">'26'</span>, <span class="hljs-string">'contact'</span> => <span class="hljs-keyword">array</span>( <span class="hljs-string">'phone'</span> => <span class="hljs-string">'13718136109'</span>, <span class="hljs-string">'address'</span> => <span class="hljs-string">'Beijing ifdoo'</span> ) ); <span class="hljs-variable">$str</span> = json_encode(<span class="hljs-variable">$arr</span>); <span class="hljs-keyword">echo</span> <span class="hljs-string">"getProfile($str)"</span>; <span class="hljs-preprocessor">?></span></span> </span></code>
HTML page calls PHP file
<script language="text/javascript" src="/xx/a.php"></script>
The echo in a.php outputs javascript code.
php page calls js file
The method in echo js in a.php is enough.
1. What is serialize
Serialize is to serialize variables and return a string expression with variable type and structure.
2. How to use serialize
Use PHP's serialize and unserialize to serialize and deserialize arrays.
<code class="hljs vbscript-html"><span class="xml"><span class="php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-variable">$arr</span> = <span class="hljs-keyword">array</span>( <span class="hljs-string">"u1"</span> => <span class="hljs-keyword">array</span>( <span class="hljs-string">"gameName"</span> => <span class="hljs-string">"德乙"</span>, <span class="hljs-string">"homeName"</span> => <span class="hljs-string">"比勒费尔德"</span>, <span class="hljs-string">"guestName"</span> => <span class="hljs-string">"不伦瑞克"</span>, <span class="hljs-string">"endTime"</span> => <span class="hljs-string">"2015-08-21"</span> ), <span class="hljs-string">"u2"</span> => <span class="hljs-keyword">array</span>( <span class="hljs-string">"gameName"</span> => <span class="hljs-string">"英超"</span>, <span class="hljs-string">"homeName"</span> => <span class="hljs-string">"水晶宫"</span>, <span class="hljs-string">"guestName"</span> => <span class="hljs-string">"阿斯顿维拉"</span>, <span class="hljs-string">"endTime"</span> => <span class="hljs-string">"2015-08-22"</span> ) ); <span class="hljs-keyword">echo</span> serialize(<span class="hljs-variable">$arr</span>); <span class="hljs-preprocessor">?></span></span> </span></code>
The result is:
a:2:{s:2:"u1";a:4:{s:8:"gameName";s:6:"Germany 2";s:8:"homeName";s:15: "Bielefeld";s:9:"guestName";s:12:"Brunswick";s:7:"endTime";s:10:"2015-08-21";}s: 2:"u2";a:4:{s:8:"gameName";s:6:"Premier League";s:8:"homeName";s:9:"Crystal Palace";s:9:"guestName" ;s:15:"Aston Villa";s:7:"endTime";s:10:"2015-08-22";}}
Among them:
a:2 indicates that this is an array with two elements;
i:0 refers to the sequence index;
a:4 means there are 4 fields;
s:8:"gameName" indicates that this is an 8-character string
Summary: PHP's serialize serializes arrays for easy storage, and data in JSON format is not only easy to store but can also be read with other languages such as javascript. If there is a lot of front-end and back-end interaction, it is recommended to use JSON. Combining PHP, Javascript, JSON and Ajax can complete powerful data interaction functions.