Re: note 11383 added to Array Functions
| From: | Jesus M. Castagnetto | Date: | Tue, 20 Feb 2001 04:00:13 +0000 |
| Subject: | Re: note 11383 added to Array Functions | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-11267@lists.php.net to get a copy of this message | ||
You could also use:
if (in_array($someindex, array_keys($somearray))) {
...
}
instead. Will add that as a note to your comment. Tnxs
for your contribution.
--- memso@memso.com wrote:
> 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
>
>
> --
> PHP Notes Mailing List (http://www.php.net/)
> To unsubscribe, e-mail:
> php-notes-unsubscribe@lists.php.net
> For additional commands, e-mail:
> php-notes-help@lists.php.net
> To contact the list administrators, e-mail:
> php-list-admin@lists.php.net
>
=====
--- Jesus M. Castagnetto <jmcastagnetto@php.net>
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year! http://personal.mail.yahoo.com/