Re: newbie array question

From: Date: Mon, 12 Mar 2001 10:39:08 +0000
Subject: Re: newbie array question
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-43448@lists.php.net to get a copy of this message
>>>>> "S" == "stas" <stas@newdel.net> writes: > Thanks, I know that, but I don't want to type out assignments > manually, as my list of my form values will grow. However, I solved > very simply like this: > $form_val_list = > "firstname,lastname,address,address2,city,state,zip,phone,email,resume"; > $form_arr = explode (",", $form_val_list); > while (list ($key, $val) = each ($form_arr)) { > $new_arr[ $val] = "some test value"; > } If you're not bothered about the initial values in the hash, you can do it with array_flip() like this: <?php $fields = explode( ',', 'firstname,lastname,city,state,phone' ); $form = array_flip( $fields ); ?> You'll end up with a hash like this: $form = array( 'firstname' => 0, 'lastname' => 1, 'city' => 2, 'state' => 3, 'phone' => 4 ); -- Robin Vickery............................................... Planet-Three, 3A West Point, Warple Way, London, W3 0RG, UK Email: robin@planet-three.net Phone: +44 (0)870 729 5444

« previous php.general (#43448) next »