I have an array of integers
Array
(
[0] => Array
(
[0] => 1531412763
[1] => 1439959339
[2] => 76
[3] => 122
[4] => 200
[5] => 4550
[6] => 444
)
...
And so on, I suppose if I look at it as if it were a database - the elements of the outermost array are the rows and the elements of the inner arrays are the columns.
I want to save that information into a file, so that I will be able to retrieve it later but I want to save it as binary data to save space. Basically if I write the first integer from the example 1531412763
to a file it will take up 10 bytes but if I could save it as a signed integer it would take up 4 bytes.
I have looked at a number of other answers which all suggest using fwrite
which I can't understand how to use in such manner?
pack
on each individual value or is there an easier way? – Spleniuspack($array)
as it is. But you can give multiple arguments to thepack
function and use repeaters (*
) after the format. Some thing likepack('i*', $int_1, $int_2,....)
for packing multiple integers. You will have to handle your array according to the format you want. – Bibulouspack('LLSSSQ', $row[0], ..., $row[6])
then write that as a single line in the file and when reading I need to useunpack
with the same format which will give me the array back. That's perfect, you can put it as an answer for someone else in my situation. – Splenius