R
R
Roman2018-04-27 18:11:38
PHP
Roman, 2018-04-27 18:11:38

How to pack an array with string values ​​using pack()?

// Создаю массив
    $aYearColor = array_fill(0,366,'#FFFFFF');	
    // Запаковываю его
    $aYearColorBlob = pack('a*', ...$aYearColor);
    // Распаковываю
    unpack('a*', $aYearColorBlob);

But this method won't work. The output will be array ( 1 => #FFFFFF );
And the error Warning: pack(): 365 arguments unused in ...
If the array is numeric, then there are no problems, everything will work:
$aYearOrder = array_fill(0,366,0);
    $aYearOrderBlob = pack('i*', ...$aYearOrder);
    unpack('i*', $aYearOrderBlob);

I read that:
For a, A, h, H, the number of repetitions determines how many characters are taken
from one data argument

But I don’t understand how then to add such an array with string values ​​​​and then unpack it back. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-04-27
@gagablik

The string does not have a fixed length, unlike other types. pack() does not add the result of string lengths or delimiters, so the result of packing strings is the same as if you had just concatenated them. Accordingly, 'a*' when packed means "one string of arbitrary length to the end".
unpack() will also not be able to automatically split the resulting string into the original substrings, it simply will not have guidelines for this.
Maybe you should switch to JSON or serialize?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question