note 11383 deleted from ref.array by jeroen
| From: | jeroen@php.net | Date: | Wed, 29 Aug 2001 17:57:03 +0000 |
| Subject: | note 11383 deleted from ref.array by jeroen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-17348@lists.php.net to get a copy of this message | ||
[Editor's note: another option is to use other array functions, i.e.:
if ( in_array($someindex, array_keys($somearray)) )
instead of the comparison to NULL]
Ever had a dynamically generated associative array, and wanted an easy way to figure out if an index
exists or not? Here's my simple solution:
// Sample Array
$somearray = array(0 => "Hi", 1 => 14, 2 => "Testing", 4 =>
"Bob");
// Does $someindex exist in $somearray?
if (gettype($somearray[$someindex])!="NULL") {
// It does exist
} else {
// It doesn't exist
}
This also works with multi-dimensional arrays:
$userarray = array{
0 => array("Name" => "Bob", "PW" => "lAm24"),
1 => array("Name" => "John", "PW" =>
"Iam24andTHE1"),
3 => array("Name" => "Joe", "PW" => "blahhalb"));
// (Not real PW data, just a sample!)
// Does the user ID exist?
if (gettype($userarray[$userid])!="NULL") {
// The User ID exists
} else {
// The User ID doesn't exist
}