PHP對象轉數組(Object轉Array),Json轉數組(Json轉Array)的方法

來源:互聯網
上載者:User

(1)php對象轉數組的方法(object 轉 array):

/** * object 轉 array */function object_to_array($obj){$_arr=is_object($obj)?get_object_vars($obj):$obj;foreach($_arr as $key=>$val){$val=(is_array($val))is_object($val)?object_to_array($val):$val;$arr[$key]=$val;}return $arr;}

(2)php Json字元轉數組的方法(json 轉 array):

如果是個 json 字串的話,可直接通過 json_decode 函數將字串轉換成數組。

json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0)

json_decode — 接受一個 JSON 格式的字串並且把它轉換為 PHP 變數

mixed json_decode(string $json[,bool $assoc])

參數:

json  待解碼的 json string 格式的字串。

assoc  當該參數為 TRUE 時,將返回一個數組(關聯陣列)形式的結果,預設為false,返回的是一個對象。

您可能感興趣的文章

  • 在php中分別使用curl的post提交資料的方法和get擷取網頁資料的方法總結
  • php將多維陣列轉換為一維數組的辦法
  • php擷取數組第一個數組單元值的方法
  • php在數組中尋找某個值是否存在(in_array(),array_search(),array_key_exists())
  • 用PHP函數memory_get_usage擷取當前PHP記憶體消耗量以實現程式的效能最佳化
  • js中將Object轉換為String函數代碼
  • PHP數組函數array_map()筆記
  • PHP解決網址URL編碼問題的函數urlencode()、urldecode()、rawurlencode()、rawurldecode()


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.