I can’t see to find an answer for this, but it’s been bugging me for a while.
When using multidimensional arrays in php (or any language for that matter), do you get any efficiency and productivity out of saving an array of a multidimensional array as its separate array or accessing that “array” element directly from the whole array?
For example,
print_r($myArray);
Array
(
[2] => Array
(
[7.0] => Array
(
[0] => 1
[1] => 23
)
[16.0] => Array
(
[0] => 4
[1] => 28
)
)
[5] => Array
(
[17.0] => Array
(
[0] => 1
[1] => 3
)
)
)
If I needed to REPEATABLY access the array of [16.0], would it be better to just save that entry as its own array until I don’t need it anymore, or is it better just to access it directly?
Option 1:
$tempArray=$myArray[2]["16.0"];
echo "Index=".$tempArray[0].";
or
Option 2:
echo "Index=".$myArray[2]["16.0"][0];
Of course this is just a tiny example, but if the values in (arbitrary) $array[.][.][n][…] are being accessed more than once, and the value of n depends on the index of a loop, is there any difference between accessing the element directly or saving that array (deep down on the layers of the array) as its own array and accessing its values that way?
If you look at the bytecode generated for these statements:
(I’ve reformatted and removed the EXT_STMT markers). You can see that the only difference is the actual assignment to $tempArray. You might think that an array assignment is expensive. However PHP uses a referencing model and does a lazy copy-on-write, so it is not, and the cost is the pretty much same whatever the array size.
(Except of course if you change elements after the assignemt, so $tempArray is no longer the same as its originating slice, and at that point the memory usage jumps as the assign triggers the clone of the slice as the references need to be split.)
OK, this approach might be worthwhile if you are doing a lot of localised readonly access to a slice to save the repeated FETCH_DIM_R lookups (the PHP compile does absolutely no local optimisation of repeated use of indexes). However, the opportunities to shoot yourself in the foot on update are significant.
Why not benchmark this yourself using a loop and
microtime()andmemory_get_usage()?