Several methods in PHP to convert stdClassObject to array (object to array). when we frequently use the API to obtain data and return json values, generally, the value obtained by parsing the json_decode method is not an array, but an object string with stdClassObjec. to obtain the corresponding PHP array, we need to use the following methods. & Nbsp; Method 1: several methods for converting stdClass Object to array (Object to array) in PHP & nbsp; stdClass & nbsp; O PHP
When we often use the API interface to retrieve the json value returned by data, the value obtained by parsing the json_decode method is generally not an array, but an object string with stdClass Objec, if we want to obtain the corresponding PHP array, we need to use the following methods.
Method 1:
// Convert PHP stdClass Object to array function object_array ($ array) {if (is_object ($ array) {$ array = (array) $ array ;} if (is_array ($ array) {foreach ($ array as $ key => $ value) {$ array [$ key] = object_array ($ value );}} return $ array ;}
Method 2:
$array = json_decode(json_encode(simplexml_load_string($xmlString)),TRUE);
Method 3:
function object2array_pre(&$object) { if (is_object($object)) { $arr = (array)($object); } else { $arr = &$object; } if (is_array($arr)) { foreach($arr as $varName => $varValue){ $arr[$varName] = $this->object2array($varValue); } } return $arr;}
If the data volume is 10 million, the execution should be 1 s, and the structure should be more complex. it can reach 3 s, resulting in poor performance.
You can replace it with the following:
function object2array(&$object) { $object = json_decode( json_encode( $object),true); return $object;}
However, the json feature can only be for utf8. otherwise, transcoding is required first.