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 \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";
?>
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.