Passing arrays by reference

From: Date: Fri, 29 Sep 2000 17:57:49 +0000
Subject: Passing arrays by reference
Groups: php.general 
Request: Send a blank email to php-general+get-18091@lists.php.net to get a copy of this message
Hi, This one shall be really basic, but I couldn't yet figure out the correct way to do it: I got the following code excerpt from http://px.sklar.com/ (http://px.sklar.com/code-pretty.html?code_id=426) and I want to simply put the arrays currently defined inside the function outside of it, and pass it to the function by reference. How should I declare the function and call it, and how should I reference the array elements inside the function (for example when using function each()). TIA, Paulo <? /////////////////////////////////// // options_array // // Chad Lieberman // mallmall@yactota.com // /////////////////////////////////// // A simple function for converting multiple arrays into html SELECT menus. // Convienient for building menus with more than one list of options. // // // Pass options_array: // // $select_name the name of the options menu // $selected a 'selected' or default key (in this example the // 'selected' option is the option previously submitted - // which may be a bit confusing) // $array an array from which to get the values of the options // menu. Out of convinience I defined the array within the // function but it should be easy to put it wherever you // like. // // If you can make this simpler, let me know . . . 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> <? print "Friend: $friends <BR> Color: $text_color\n"; ?> </BODY> </HTML>

« previous php.general (#18091) next »