繁体   English   中英

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

[英]What is the fastest way to convert an object to an array in PHP?

当前,有很多不同的方法可以使用PHP将多层对象转换为多维数组。 有些似乎适得其反,但被广泛使用。 我真的想知道哪种方法最快(通常)?

我已经尝试了几种最常用的方法并对结果进行计时。 我意识到对象的深度会产生很大的影响,每个级别的子对象的数量也会影响很大。 我很好奇是否有人能以更快的速度进行思考。 下面是我使用的代码,据我所知,这是两种最常见的方法。 我需要一些样本数据,所以我从一个示例XML文件中提取了它。

<?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 \n";
echo "Method 2 time to execute: $method2ExecTime \n";
echo "Method 3 time to execute: $method3ExecTime \n";
echo "Method 4 time to execute: $method4ExecTime \n";


?>

我在同一台卸载的测试服务器上运行了3次测试。 结果如下:

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

编辑:添加了另一种涉及使用递归类型转换的方法。

编辑:添加了数组映射方法。 这是相当快的最快速度。

json_encode + json_decode方法的快速性来自于以下事实:这两个函数均具有已编译的本机实现,因此其运行速度比所需的PHP替代方法快得多。 与数组强制转换类似,它也可以提高速度,但存在不能超过一个级别的局限性。

另外,每次调用PHP编码函数都需要设置一个PHP堆栈,这比设置本机(C / C ++)堆栈要昂贵得多。

结论是,将提供一个object2array()函数的PHP扩展将是最快的扩展,但是我不确定这种方法是否存在。 在找到这种方法之前,json函数家族仍然是最快的一种。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM