Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@janeklb
Last active September 4, 2020 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janeklb/4761535 to your computer and use it in GitHub Desktop.
Save janeklb/4761535 to your computer and use it in GitHub Desktop.
PHP: array to object
<?php
# Simple loop copy
function loop_copy($array) {
$object = new stdClass();
foreach ($array as $key => $value) {
$object->$key = $value;
}
}
# leveraging json_decode + json_encode
function json_copy($array) {
json_decode(json_encode($array), FALSE);
}
# http://pear.php.net/package/Benchmark/
# http://pear.php.net/package/Benchmark/docs/1.2.9/Benchmark/Benchmark_Timer.html
require 'Benchmark/Timer.php';
# prepare the arrays to be copied
$sizes = array(5, 20, 100, 500);
$arrays = array();
foreach ($sizes as $size) {
$array = array();
$i = $size;
while ($i-- >= 0) {
$key = uniqid('key');
$array[$key] = uniqid('value');
}
$arrays[] = $array;
}
# Prepare timer and iterations
$itera = array(100, 500, 1000);
foreach ($itera as $i) {
foreach ($arrays as $array) {
$array_len = count($array);
$timer = new Benchmark_Timer(TRUE);
$x = $i;
while ($x-- >= 0) {
json_copy($array);
}
$timer->setMarker("json_copy with $i copies of array size $array_len");
$x = $i;
while ($x-- >= 0) {
loop_copy($array);
}
$timer->setMarker("loop_copy with $i copies of array size $array_len");
$timer->display();
}
}
@metalmatze
Copy link

Could you post some results for this?

@craigwaterman
Copy link

@metalmatze these are the results from my Macbook 2,2 w/ PHP 5.3.15 (i7 @ 2.2Ghz)

----------------------------------------------------------------------------------------
marker                                      time index            ex time         perct   
----------------------------------------------------------------------------------------
Start                                       1378258964.24351800   -                0.00%
----------------------------------------------------------------------------------------
json_copy with 100 copies of array size 6   1378258964.24449400   0.000976        79.16%
----------------------------------------------------------------------------------------
loop_copy with 100 copies of array size 6   1378258964.24472600   0.000232        18.82%
----------------------------------------------------------------------------------------
Stop                                        1378258964.24475100   0.000025         2.03%
----------------------------------------------------------------------------------------
total                                       -                     0.001233       100.00%
----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
marker                                       time index            ex time         perct   
-----------------------------------------------------------------------------------------
Start                                        1378258964.24495700   -                0.00%
-----------------------------------------------------------------------------------------
json_copy with 100 copies of array size 21   1378258964.24794700   0.002990        81.34%
-----------------------------------------------------------------------------------------
loop_copy with 100 copies of array size 21   1378258964.24862200   0.000675        18.36%
-----------------------------------------------------------------------------------------
Stop                                         1378258964.24863300   0.000011         0.30%
-----------------------------------------------------------------------------------------
total                                        -                     0.003676       100.00%
-----------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
marker                                        time index            ex time         perct   
------------------------------------------------------------------------------------------
Start                                         1378258964.24883700   -                0.00%
------------------------------------------------------------------------------------------
json_copy with 100 copies of array size 101   1378258964.26249600   0.013659        82.24%
------------------------------------------------------------------------------------------
loop_copy with 100 copies of array size 101   1378258964.26543100   0.002935        17.67%
------------------------------------------------------------------------------------------
Stop                                          1378258964.26544600   0.000015         0.09%
------------------------------------------------------------------------------------------
total                                         -                     0.016609       100.00%
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
marker                                        time index            ex time         perct   
------------------------------------------------------------------------------------------
Start                                         1378258964.26563400   -                0.00%
------------------------------------------------------------------------------------------
json_copy with 100 copies of array size 501   1378258964.33470900   0.069075        82.69%
------------------------------------------------------------------------------------------
loop_copy with 100 copies of array size 501   1378258964.34914800   0.014439        17.29%
------------------------------------------------------------------------------------------
Stop                                          1378258964.34916700   0.000019         0.02%
------------------------------------------------------------------------------------------
total                                         -                     0.083533       100.00%
------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
marker                                      time index            ex time         perct   
----------------------------------------------------------------------------------------
Start                                       1378258964.34941700   -                0.00%
----------------------------------------------------------------------------------------
json_copy with 500 copies of array size 6   1378258964.35392300   0.004506        78.58%
----------------------------------------------------------------------------------------
loop_copy with 500 copies of array size 6   1378258964.35513400   0.001211        21.12%
----------------------------------------------------------------------------------------
Stop                                        1378258964.35515100   0.000017         0.30%
----------------------------------------------------------------------------------------
total                                       -                     0.005734       100.00%
----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
marker                                       time index            ex time         perct   
-----------------------------------------------------------------------------------------
Start                                        1378258964.35527000   -                0.00%
-----------------------------------------------------------------------------------------
json_copy with 500 copies of array size 21   1378258964.36951100   0.014241        80.84%
-----------------------------------------------------------------------------------------
loop_copy with 500 copies of array size 21   1378258964.37286600   0.003355        19.04%
-----------------------------------------------------------------------------------------
Stop                                         1378258964.37288700   0.000021         0.12%
-----------------------------------------------------------------------------------------
total                                        -                     0.017617       100.00%
-----------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
marker                                        time index            ex time         perct   
------------------------------------------------------------------------------------------
Start                                         1378258964.37306300   -                0.00%
------------------------------------------------------------------------------------------
json_copy with 500 copies of array size 101   1378258964.43979100   0.066728        82.40%
------------------------------------------------------------------------------------------
loop_copy with 500 copies of array size 101   1378258964.45402100   0.014230        17.57%
------------------------------------------------------------------------------------------
Stop                                          1378258964.45404100   0.000020         0.02%
------------------------------------------------------------------------------------------
total                                         -                     0.080978       100.00%
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
marker                                        time index            ex time         perct   
------------------------------------------------------------------------------------------
Start                                         1378258964.45423700   -                0.00%
------------------------------------------------------------------------------------------
json_copy with 500 copies of array size 501   1378258964.78209200   0.327855        82.23%
------------------------------------------------------------------------------------------
loop_copy with 500 copies of array size 501   1378258964.85294000   0.070848        17.77%
------------------------------------------------------------------------------------------
Stop                                          1378258964.85296100   0.000021         0.01%
------------------------------------------------------------------------------------------
total                                         -                     0.398724       100.00%
------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
marker                                       time index            ex time         perct   
-----------------------------------------------------------------------------------------
Start                                        1378258964.85311400   -                0.00%
-----------------------------------------------------------------------------------------
json_copy with 1000 copies of array size 6   1378258964.86216500   0.009051        79.37%
-----------------------------------------------------------------------------------------
loop_copy with 1000 copies of array size 6   1378258964.86450800   0.002343        20.55%
-----------------------------------------------------------------------------------------
Stop                                         1378258964.86451700   0.000009         0.08%
-----------------------------------------------------------------------------------------
total                                        -                     0.011403       100.00%
-----------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
marker                                        time index            ex time         perct   
------------------------------------------------------------------------------------------
Start                                         1378258964.86468300   -                0.00%
------------------------------------------------------------------------------------------
json_copy with 1000 copies of array size 21   1378258964.89393300   0.029250        81.97%
------------------------------------------------------------------------------------------
loop_copy with 1000 copies of array size 21   1378258964.90034600   0.006413        17.97%
------------------------------------------------------------------------------------------
Stop                                          1378258964.90036600   0.000020         0.06%
------------------------------------------------------------------------------------------
total                                         -                     0.035683       100.00%
------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
marker                                         time index            ex time         perct   
-------------------------------------------------------------------------------------------
Start                                          1378258964.90060900   -                0.00%
-------------------------------------------------------------------------------------------
json_copy with 1000 copies of array size 101   1378258965.03739800   0.136789        82.91%
-------------------------------------------------------------------------------------------
loop_copy with 1000 copies of array size 101   1378258965.06556800   0.028170        17.08%
-------------------------------------------------------------------------------------------
Stop                                           1378258965.06558600   0.000018         0.01%
-------------------------------------------------------------------------------------------
total                                          -                     0.164977       100.00%
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
marker                                         time index            ex time         perct   
-------------------------------------------------------------------------------------------
Start                                          1378258965.06592200   -                0.00%
-------------------------------------------------------------------------------------------
json_copy with 1000 copies of array size 501   1378258965.71811300   0.652191        81.86%
-------------------------------------------------------------------------------------------
loop_copy with 1000 copies of array size 501   1378258965.86263200   0.144519        18.14%
-------------------------------------------------------------------------------------------
Stop                                           1378258965.86266200   0.000030         0.00%
-------------------------------------------------------------------------------------------
total                                          -                     0.796740       100.00%
-------------------------------------------------------------------------------------------

@franciscop
Copy link

A small note must be done here: loop_copy($array) is not recursive, while the json_copy($array) is.

@nidhviadmin
Copy link

If we change line #7 from
$object->$key = $value
into
$object->$key = (is_array($value) ? loop_copy($value) : $value)

Then the loop_copy will be recursive. Correct me If I'm wrong.

@janeklb
Copy link
Author

janeklb commented Sep 4, 2020

You'd have to make a few other changes to make it truly recursive (ie, handle is_object($value) too) but yeah -- could totally make it recursive

That said bear in mind :)
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment