<?php
$select = [
'foo',
'bar', [
'size' => 128,
'type' => 'png'
],
'baz',
'qux'
];
function convert($in) {
$out = [];
for ($i = 0, $m = count($in); $i < $m; $i++) {
$current = $in[$i];
$next = $in[$i + 1] ?? null;
if (!is_string($current)) {
throw new \Exception('Malformed field list.');
}
if (is_array($next)) {
$out[$current] = $next;
$i++;
} else {
$out[$current] = true;
}
}
return $out;
}
var_dump(convert($select));
- Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 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.29, 8.2.0 - 8.2.21, 8.3.0 - 8.3.9
- array(4) {
["foo"]=>
bool(true)
["bar"]=>
array(2) {
["size"]=>
int(128)
["type"]=>
string(3) "png"
}
["baz"]=>
bool(true)
["qux"]=>
bool(true)
}
- Output for 5.6.0 - 5.6.40
- Parse error: syntax error, unexpected '?' in /in/a1094 on line 16
Process exited with code 255.
preferences:
178.25 ms | 403 KiB | 298 Q