3v4l.org

run code in 300+ PHP versions simultaneously
<?php function validateAndSanitize(array &$input, array $structure): bool { if (array_diff_key($structure, $input)) { return false; // missing required column(s) } $input = array_intersect_key($input, $structure); // prune unexpected columns return true; } $structure = ['id' => '', 'name' => '', 'quantity' => '']; $array = [ 'id' => 'GFKHF312', 'name' => 'Item', 'quantity' => 1, 'new_key' => 120, 'other_key' => 'fsdfs', ]; if (!validateAndSanitize($array, $structure)) { echo "bonk\n"; } var_export($array);
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array ( 'id' => 'GFKHF312', 'name' => 'Item', 'quantity' => 1, )

preferences:
73.65 ms | 402 KiB | 62 Q