Re: Passing arrays by reference
| From: | php at brazilinfo dot com | Date: | Fri, 29 Sep 2000 18:57:53 +0000 |
| Subject: | Re: Passing arrays by reference | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-18098@lists.php.net to get a copy of this message | ||
"Frank M. Kromann" <fmk@swwwing.com> said:
> Hi,
>
> When you define the function like thisÆ
>
> function options_array ($select_name,$selected,&$array) {
> }
>
> $array will be pased by reference.
>
> You will access the variable as any other variable but remember that any
changes made to the variable will remain after the function ends. That
includes the internal pointer of an array, used ad affected by each().
>
> - Frank
>
Thanks! My failure was not to notice that the author was passing the name of
the array, so that he had to use $$array inside the function, which would
obviously not work when passing a reference. The code with the arrays taken
out of the function would then read:
-------------------------------
<?
$colors = array ( "#FFFFFF" => "White",
"#000000" => "Black",
"#99CC66" => "Green",
"#FF9999" => "Red",
"#996633" => "Brown",
"#FFFFCC" => "Yellow",
"#CCFFFF" => "Blue");
$friends = array ( "John Entwhistle" => "Johnny",
"Mary Tyler Moore" => "Mary",
"Charlie Parker" => "Bird",
"Merryl Stubing" => "Captain");
function options_array ($select_name,$selected,&$array)
{
print "<SELECT NAME=\"$select_name\">\n";
while (list ($key, $val) = each ($array))
{
print "<option value= " . "\"$key\" " .
($key==$selected? " selected ": ""). ">".$val. "\n";
}
print "</SELECT>\n";
}
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<H2> Example: </H2><BR>
<H1> Choose a color and a friend. </H1>
<FORM TYPE = "POST" ACTION = "options_array.php">
<?
options_array ( "friends", $friends, $friends);
options_array ( "text_color", $text_color, $colors);
?>
<INPUT TYPE = "SUBMIT" VALUE = "SUBMIT">
</FORM>
</BODY>
</HTML>
-------------------------------
Instead of:
> >function options_array ($select_name,$selected,$array)
> >{
> > $colors = array ( "#FFFFFF" => "White",
> > "#000000" => "Black",
> > "#99CC66" => "Green",
> > "#FF9999" => "Red",
> > "#996633" => "Brown",
> > "#FFFFCC" => "Yellow",
> > "#CCFFFF" => "Blue");
> >
> > $friends = array ( "John Entwhistle"
=> "Johnny",
> > "Mary Tyler Moore" =>
> > "Mary",
> > "Charlie Parker" =>
> > "Bird",
> > "Merryl Stubing"
=> "Captain");
> >
> > print "<SELECT NAME=\"$select_name\">\n";
> >
> > while (list ($key, $val) = each ($$array))
> > {
> > print "<option value= " . "\"$key\" " .
> >($key==$selected? " selected ": ""). ">".$val.
> >"\n";
> > }
> >
> > print "</SELECT>\n";
> >}
> >
> >?>
> >
> >
> ><HTML>
> ><HEAD>
> ></HEAD>
> ><BODY>
> ><H2> Example: </H2><BR>
> ><H1> Choose a color and a friend. </H1>
> >
> ><FORM TYPE = "POST" ACTION = "options_array.php">
> >
> ><?
> > options_array ( "friends", $friends, "friends");
> > options_array ( "text_color", $text_color, "colors");
> >?>
> >
> ><INPUT TYPE = "SUBMIT" VALUE = "SUBMIT">
> ></FORM>
> >
> ></BODY>
> ></HTML>