Skip to content

Instantly share code, notes, and snippets.

@lastguest
Created September 7, 2018 12:52
Show Gist options
  • Save lastguest/01d25b1d98c4537c3eaf958130d48cc0 to your computer and use it in GitHub Desktop.
Save lastguest/01d25b1d98c4537c3eaf958130d48cc0 to your computer and use it in GitHub Desktop.
[PHP] array_mask
<?php
function array_mask(array $unsafe, array $mask){
return array_intersect_key(array_merge($mask, $unsafe), $mask);
}
$maschera_default = [
'name' => 'Senza Nome',
'surname' => 'Senza Cognome',
'age' => 0,
];
$data = [
'surname' => 'Rossi',
'age' => 99,
'io_non_ci_devo_essere' => 123,
'io_neppure' => "blablabla"
];
print_r(
array_mask($data, $maschera_default)
);
/*
Array
(
[name] => Senza Nome
[surname] => Rossi
[age] => 99
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment