<?php
$defaults = [
"shoes" => 'no',
"pants" => 'no',
"skirt" => 'no',
"socks" => 'no',
"jacket" => 'no',
];
$_POST['id'] = '1';
$_POST['clothes'] = "shoes, socks, jacket";
$ticked = array_fill_keys(explode(', ', $_POST['clothes']), 'yes');
$yesses = array_intersect_key($ticked, $defaults);
$row = ['id' => (int) $_POST['id']] + array_replace($defaults, $yesses);
var_export($row);
- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- array (
'id' => 1,
'shoes' => 'yes',
'pants' => 'no',
'skirt' => 'no',
'socks' => 'yes',
'jacket' => 'yes',
)
preferences:
50.7 ms | 402 KiB | 62 Q