Home>Article>Backend Development> How to convert the json object obtained by curl into an array
How to convert the json object obtained by curl into an array?
Today I will share with you a method of using php curl to obtain a json object and convert it into an array. It has a good reference value and I hope it will be helpful to everyone.
Example:
function objtoarr($obj){ $ret = array(); foreach($obj as $key =>$value){ if(gettype($value) == 'array' || gettype($value) == 'object'){ $ret[$key] = objtoarr($value); }else{ $ret[$key] = $value; } } return $ret; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,'http://www.tudou.com/albumcover/albumdata/getAlbumItems.html?acode=pEFBZGfERLo&charset=utf-8'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_ENCODING, "gzip"); $output = curl_exec($ch); curl_close($ch); $content = json_decode($output); $content_arr = objtoarr($content); var_dump($content_arr);
Related recommendations:php tutorial
The above is the detailed content of How to convert the json object obtained by curl into an array. For more information, please follow other related articles on the PHP Chinese website!