如何将curl获取到的json对象转成数组

如何将curl获取到的json对象转成数组?

今天为大家分享一篇php curl获取到json对象并转成数组array的方法,具有很好的参考价值,希望对大家有所帮助。

例子:

  1. function objtoarr($obj){
  2. $ret = array();
  3. foreach($obj as $key =>$value){
  4. if(gettype($value) == 'array' || gettype($value) == 'object'){
  5. $ret[$key] = objtoarr($value);
  6. }else{
  7. $ret[$key] = $value;
  8. }
  9. }
  10. return $ret;
  11. }
  12. $ch = curl_init();
  13. curl_setopt($ch, CURLOPT_URL,'http://www.tudou.com/albumcover/albumdata/getAlbumItems.html?acode=pEFBZGfERLo&charset=utf-8');
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  15. curl_setopt($ch, CURLOPT_HEADER, 0);
  16. curl_setopt($ch, CURLOPT_ENCODING, "gzip");
  17. $output = curl_exec($ch);
  18. curl_close($ch);
  19. $content = json_decode($output);
  20. $content_arr = objtoarr($content);
  21. var_dump($content_arr);