: This article mainly introduces several methods for converting stdClassObject to array in PHP. For more information about PHP tutorials, see. The communication between PHP and JS usually uses json, but the array transmitted using json is not a standard array, but a stdClass type. Then we can refer to the following methods for conversion.
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:
The code is as follows:
$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.
The above describes several methods for converting stdClass Object to array in PHP, including some content, and hope to help those who are interested in PHP tutorials.