PHP提供函數直接將array轉換成json
<code>json_encode(<span>$param_array</span>);</code>
若需要下面這種嵌套式如何處理呢?
<code>{ <span>"appid"</span>: <span>"1500001105"</span>, <span>"nonce"</span>: <span>32490</span>, <span>"timestamp"</span>: <span>1458195253</span>, <span>"signature"</span>: <span>"bf13fe77f46a96edba1a0e2b84fce66580b2f50b"</span>, <span>"param"</span>: { <span>"userid"</span>: <span>"bonnie"</span> } }</code>
先構成一個二級array,再呼叫json_encode轉換即可。
<code><span>$param</span> = <span>array</span>( <span>'userid'</span>=><span>'bonnie'</span> ); <span>$params</span> = <span>array</span>( <span>'appid'</span>=><span>'1500001105'</span>, <span>'nonce'</span>=><span>$nonce</span>, <span>'timestamp'</span>=><span>$timestamp</span>, <span>'signature'</span>=><span>$signature</span>, <span>'param'</span>=><span>$param</span>, ); <span>$params_json</span> = json_encode(<span>$params</span>);</code>
以上就介紹了PHP 二級array轉換json,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。