json在php中的使用之如何轉換json為數組


 

復制代碼
<?php
   $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

   var_dump(json_decode($json));
   echo"<br/>";
   var_dump(json_decode($json, true));
?>
復制代碼

數組$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';被json_decode()解碼,轉換回來的數據是對象,var_dump(json_decode($json))得到的是一個對象,如下:

1
object(stdClass)#1 (5) { [ "a" ]=> int(1) [ "b" ]=> int(2) [ "c" ]=> int(3) [ "d" ]=> int(4) [ "e" ]=> int(5) }

  那么,要怎么樣才能把json數組轉換為php格式的數組呢,采用以下方式:

json_decode($json, true)

    這樣得到的數據就是php的數組了:

1
var_dump(json_decode( $json , true));

  效果如下:

array(5) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM