Skip to content

Instantly share code, notes, and snippets.

@Keirua
Created December 13, 2016 07:53
Show Gist options
  • Save Keirua/35a32326606368a6a3beb7b4ead01803 to your computer and use it in GitHub Desktop.
Save Keirua/35a32326606368a6a3beb7b4ead01803 to your computer and use it in GitHub Desktop.
Utilisez array_key_exists !
<?php
$a = ['AB' => 12, 'CB' => 34];
echo "Cas nominal : La clé est présente dans le tableau".PHP_EOL;
echo (int)array_key_exists('AB', $a).PHP_EOL;
echo (int)isset($a['AB']).PHP_EOL;
echo (int)!empty($a['AB']).PHP_EOL;
echo "Cas nominal : La clé n'est pas présente dans le tableau".PHP_EOL;
echo (int)array_key_exists('YZ', $a).PHP_EOL;
echo (int)isset($a['YZ']).PHP_EOL;
echo (int)!empty($a['YZ']).PHP_EOL;
echo "Cas limite : la clé est présente mais vaut 0".PHP_EOL;
$b = ['EF' => 0];
echo (int)array_key_exists('EF', $b).PHP_EOL;
echo (int)isset($b['EF']).PHP_EOL;
echo (int)!empty($b['EF']).PHP_EOL;
echo "Cas limite : la clé est présente mais vaut null".PHP_EOL;
$c = ['GH' => null];
echo (int)array_key_exists('GH', $c).PHP_EOL;
echo (int)isset($c['GH']).PHP_EOL;
echo (int)!empty($c['GH']).PHP_EOL;
echo "Cas limite : Le tableau n'existe pas".PHP_EOL;
echo (int)array_key_exists('IJ', $d).PHP_EOL;
echo (int)isset($d['IJ']).PHP_EOL;
echo (int)!empty($d['IJ']).PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment