Maison > Article > développement back-end > PHP 二级array转换json
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>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介绍了PHP 二级array转换json,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。