I need an efficient way to deal with 3D matrices in PHP. Numeric indices only, all data will be doubles but can be changed to integers if really needed.
Is there a portable way to do this efficiently ?
Yes ! I know PHP is not the obvious choice for number crunching, but this is a hard constraint, I can’t change that.
Efficient data representation and PHP don’t usually appear in the same sentence, so the going will definitely not be easy.
If you are only going to be using numeric indexes then
SplFixedArrayis a sizable improvement over the built-in array type; the benefits are going to be in memory footprint first and performance second.You could use a typical nested array scheme to represent the 3D matrix, or you could also use a single-dimensional array and translate between (x, y, z) to (i) manually. In the latter case you should expect memory footprint to decrease somewhat, but performance might also suffer.