How to use pack() to pack an array with string values?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
10 views
// Create the array $aYearColor = array_fill(0,366,'#FFFFFF'); // Pack it $aYearColorBlob = pack('a*', ...$aYearColor); // Unpack unpack('a*', $aYearColorBlob);


But this method will not work. The output will be array ( 1 => #FFFFFF );

And the error is Warning: pack(): 365 arguments unused in ...

If the array is numeric, then there is no problem, everything works:

$aYearOrder = array_fill(0,366,0); $aYearOrderBlob = pack('i*', ...$aYearOrder); unpack('i*', $aYearOrderBlob);

Read that:

For a, a, h, h the number of repetitions determines how many characters are taken
from one data argument


But I do not understand how to add this array with string values and then back to unpack. Thank you
by | 10 views

1 Answer

0 like 0 dislike
The string has a fixed length, unlike other types. pack() adds the result, no string lengths, no delimiters, therefore, the result of the packaging lines will be the same as if you just combined them. Accordingly, 'a*' if the packaging indicates "one line of arbitrary length to the end."
unpack() is also not able to automatically split the resulting string on the source substring, it will not be simple for this guidelines.
Maybe you should switch to JSON or serialize?
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
35,405 users