<?php
function pack_array_of_bools_to_string(array $bools): string
{
$str = '';
$l = count($bools);
for ($i = 0; $i < $l; ++$i) {
$str .= $bools ? '1' : '0';
}
return $str;
}
function unpack_string_to_array_of_bools(string $packed): array
{
$arr = [];
$l = strlen($packed) - 1;
for ($i = 0; $i < $l; ++$i) {
$arr[] = $packed[$i] === '1';
}
return $arr;
}
$arr = [
true, true, false, true, false, false, true, true, false, false, false, true, false
];
var_dump(
pack_array_of_bools_to_string($arr),
unpack_string_to_array_of_bools('1101001100010')
);
- Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
- string(13) "1111111111111"
array(12) {
[0]=>
bool(true)
[1]=>
bool(true)
[2]=>
bool(false)
[3]=>
bool(true)
[4]=>
bool(false)
[5]=>
bool(false)
[6]=>
bool(true)
[7]=>
bool(true)
[8]=>
bool(false)
[9]=>
bool(false)
[10]=>
bool(false)
[11]=>
bool(true)
}
- Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
- Parse error: syntax error, unexpected ':', expecting '{' in /in/lAKTt on line 3
Process exited with code 255.
preferences:
216.24 ms | 402 KiB | 294 Q