dongpa5207 2015-04-10 19:05
浏览 85
已采纳

在PHP中将对象转换为数组的最快方法是什么?

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.

<?php

$xml = file_get_contents("http://www.w3schools.com/xml/cd_catalog.xml");

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


/*
    Method 1
    Recursive typecasting
    http://ben.lobaugh.net/blog/567/php-recursively-convert-an-object-to-an-array
*/

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 http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array
*/

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

    $res_arr = array(); 

    if (!empty($object)) { 

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

        $i=0; 
        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; 
            } 

        $i++; 
        }

    } 

    return $res_arr;
}

$method3StartTime = microtime(true);

$method3Results = object_to_array_recursive($xmlObj);

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



/*
    Method 4
    Array map method
    http://stackoverflow.com/questions/2476876/how-do-i-convert-an-object-to-an-array/2476954#2476954
*/

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!