Skip to content

Instantly share code, notes, and snippets.

@trivoallan
Created January 21, 2015 10:05
Show Gist options
  • Save trivoallan/acd2a01eb1c277f56c50 to your computer and use it in GitHub Desktop.
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
<?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