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))獲得的是一個對象,以下:php

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

  那麼,要怎麼樣才能把json數組轉換爲php格式的數組呢,採用如下方式:json

json_decode($json, true)

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

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

  效果以下:post

array(5) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) }
相關文章
相關標籤/搜索