In the object-PHP array of several methods

PHP in the array of objects go a few ways:

1.
//PHP stdClass Object转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;  
}
2.
$array = json_decode(json_encode(simplexml_load_string($xmlString)),TRUE);
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 ] = $ the this -> object2array ( $ varValue ); 
        } 
    } 
    return  $ ARR ; 
}
 4. If the amount of data to 10W then performed to get into 1s, the structure then complex, can be achieved 3s, poor performance, and may be replaced with the following:
 function object2array (& $ Object ) {
     $ Object = of json_decode (json_encode ( $ Object ), to true );
     return   $ Object ; 
}

 

json_decode(json_encode($array),true)

 

Guess you like

Origin www.cnblogs.com/bluealine/p/11040928.html