2015-04-10 19:05
浏览 85


Currently there are quite a few different ways to convert a multi-layered object into a multidimensional array using PHP. Some seem pretty counterproductive but are widely used. I would really like to know which method is fastest (in general)?

I have experimented with several of the most common methods and timed the results. I realize that the depth of the object will have big effects and so will the number of sub-objects at each level. I am curious if anyone has a way that they think is faster. Below is my code using from what I can tell are the two most common methodologies. I needed some sample data so I pulled it from an example XML file.


$xml = file_get_contents("");

//load XML string into SimpleXML object
$xmlObj = simplexml_load_string($xml);

    Method 1
    Recursive typecasting

function objToArrayRecursiveTypecast($obj)
    if(is_object($obj)) $obj = (array) $obj;
    if(is_array($obj)) {
        $new = array();
        foreach($obj as $key => $val) {
            $new[$key] = objToArrayRecursiveTypecast($val);
    else $new = $obj;
    return $new;       

$method1StartTime = microtime(true);

$method1Results = objToArrayRecursiveTypecast($xmlObj);

$method1EndTime = microtime(true);
$method1ExecTime = $method1EndTime - $method1StartTime;

    Method 2
    json_encode json_decode
    Appears in code everywhere

$method2StartTime = microtime(true);

$method2Results = json_decode(json_encode($xmlObj), true);

$method2EndTime = microtime(true);
$method2ExecTime = $method2EndTime - $method2StartTime;

    Method 3
    Recursive object read and array assignment
    Answer in

function object_to_array_recursive( $object, $assoc=TRUE, $empty='' ) 

    $res_arr = array(); 

    if (!empty($object)) { 

        $arrObj = is_object($object) ? get_object_vars($object) : $object;

        foreach ($arrObj as $key => $val) { 
            $akey = ($assoc !== FALSE) ? $key : $i; 
            if (is_array($val) || is_object($val)) { 
                $res_arr[$akey] = (empty($val)) ? $empty : object_to_array_recursive($val); 
            else { 
                $res_arr[$akey] = (empty($val)) ? $empty : (string)$val; 



    return $res_arr;

$method3StartTime = microtime(true);

$method3Results = object_to_array_recursive($xmlObj);

$method3EndTime = microtime(true);
$method3ExecTime = $method3EndTime - $method3StartTime;

    Method 4
    Array map method

function arrayMapObjectToArray($object)
    if(!is_object($object) && !is_array($object))
        return $object;

    return array_map('arrayMapObjectToArray', (array) $object);

$method4StartTime = microtime(true);

$method4Results = arrayMapObjectToArray($xmlObj);

$method4EndTime = microtime(true);
$method4ExecTime = $method4EndTime - $method4StartTime;

//output results
echo "Method 1 time to execute: $method1ExecTime 
echo "Method 2 time to execute: $method2ExecTime 
echo "Method 3 time to execute: $method3ExecTime 
echo "Method 4 time to execute: $method4ExecTime 


I ran the test 3 times on the same unloaded test server. The results follow:

Method 1 time to execute: 0.00066113471984863
Method 2 time to execute: 0.00059700012207031
Method 3 time to execute: 0.00090503692626953
Method 4 time to execute: 0.00050783157348633

Method 1 time to execute: 0.00066494941711426
Method 2 time to execute: 0.00057506561279297
Method 3 time to execute: 0.00089788436889648
Method 4 time to execute: 0.00052714347839355

Method 1 time to execute: 0.00067400932312012
Method 2 time to execute: 0.00057005882263184
Method 3 time to execute: 0.0009000301361084
Method 4 time to execute: 0.00051212310791016

EDIT: Added another method that involves using recursive typecasting.

EDIT: Added array map method. It is the fastest by a considerable margin.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchenbiao0916 2015-04-11 19:02

    The speediness of the json_encode+json_decode approach comes from the fact that both functions have native implementations that are already compiled thus run much faster than the needed-to-interpret-by-php alternatives. Similar with the array cast, which also gives you speed, but has the limitation of not going deeper than one level.

    Also, each call to a PHP coded function will need to setup a PHP stack, which is more expensive than setting up a native (C/C++) stack.

    As conclusion, a PHP extension that will provide a object2array() function will be the fastest one, however I am not sure such a method exists. Until such a method is found the json function family remains the fastest one.

    打赏 评论

相关推荐 更多相似问题