note 11383 added to Array Functions
| From: | memso at memso dot com | Date: | Mon, 19 Feb 2001 00:23:56 +0000 |
| Subject: | note 11383 added to Array Functions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-11229@lists.php.net to get a copy of this message | ||
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
}
http://www.php.net/manual/en/ref.array.php