note 27464 added to function.array
| From: | grubby_d at yahoo dot com | Date: | Fri, 06 Dec 2002 16:58:04 +0000 |
| Subject: | note 27464 added to function.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40431@lists.php.net to get a copy of this message | ||
About NULL as an array index.
An interesting thing with arrays is that you can use NULL as an index. I am trying it out with drop
down list which will be used to update a database. Its not that good of an idea but it made me find
the solution. For the database example you want to use the index "NULL" with quotes.
Say you have table person which has a foreign key reference to companies. BUT you want to allow the
user to not specify a company as well. So you have determined that the database reference allows
NULLs.
So you make a SELECT control with the lookup values as:
<OPTION value=(comp_id)>comp_name</OPTION>
using a while loop to print out the values.
Then you want the option to select NONE of the options. If you use something like -1 or 0 to
represent this "blank" option you have to handle that in your php. Instead add this to
your array: myarray("NULL") = "-none-" and you will get a field like this:
<OPTION value=NULL>-none-</OPTION>
Now every value from your SELECT control will be valid for the database and wont cause a foreign key
references violation. it doesnt guarantee the data is coming from your trusty SELECT box so you
still may want to check anyway.
Some interesting things about using the real NULL value as an array index:
$myarray = array(1, 2, 3);
echo count($myarray) . "<BR>"; // 3
$myarray[NULL] = "the null value";
echo count($myarray) . "<BR>"; // 4
if (array_key_exists(NULL, $myarray)
{ echo "this code will never be reached";}
This will return FALSE and will generate this warning:
Warning: Wrong datatype for first argument in call to array_key_exists
--
http://www.php.net/manual/en/function.array.php
http://master.php.net/manage/user-notes.php?action=edit+27464
http://master.php.net/manage/user-notes.php?action=delete+27464
http://master.php.net/manage/user-notes.php?action=reject+27464