Created
January 21, 2015 10:05
-
-
Save trivoallan/acd2a01eb1c277f56c50 to your computer and use it in GitHub Desktop.
En finir avec les E_NOTICE dues à l'accès à des clés de tableau inexistantes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Ce tableau contient toutes les clés possibles | |
$defaults = array( | |
'key1' => null, | |
'key2' => 42, | |
'key3' => 'raoul' | |
); | |
// C'est le tableau qu'on récupére en entrée. Il contient un certain nombre des clés possibles | |
$input = array( | |
'key3' => 'alain' | |
); | |
// On merge les deux tableaux | |
$safeInput = array_merge($defaults, $input); | |
// On obtient un tableau qui contient toutes les clés possibles avec les valeurs par défaut | |
// surchargées par les valeurs en entrée | |
var_dump($safeInput); | |
/* | |
array(3) { | |
'key1' => | |
NULL | |
'key2' => | |
int(42) | |
'key3' => | |
string(5) "alain" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment