note 11383 modified in Array Functions by jmcastagnetto
| From: | jmcastagnetto@php.net | Date: | Tue, 20 Feb 2001 03:58:35 +0000 |
| Subject: | note 11383 modified in Array Functions by jmcastagnetto | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-11266@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
}
http://www.php.net/manual/array-functions.php