[PHP]assigning a value to objects in an array

hey all,

I have an array that i am using to populate a dropdown field of a form, what i want to do is assign the value tag a number of which position the state name is in the array …

[php]$state = array(
1 => array(‘Alabama’,‘Alaska’,‘Arizona’,‘Arkansas’,‘California’,‘Colorado’,‘Connecticut’,‘Delaware’,‘District of Columbia’,‘Florida’,‘Georgia’,‘Hawaii’,‘Idaho’,‘Illinois’,‘Indiana’,‘Iowa’,‘Kansas’,‘Kentucky’,‘Louisiana’,‘Maine’,‘Montana’,‘Nebraska’,‘Nevada’,‘New Hampshire’,‘New Jersey’,‘New Mexico’,‘New York’,‘North Carolina’,‘North Dakota’,‘Ohio’,‘Oklahoma’,‘Oregon’,‘Maryland’,‘Massachusetts’,‘Michigan’,‘Minnesota’,‘Mississippi’,‘Missouri’,‘Pennsylvania’,‘Rhode Island’,‘South Carolina’,‘South Dakota’,‘Tennessee’,‘Texas’,‘Utah’,‘Vermont’,‘Virginia’,‘Washington’,‘West Virginia’,‘Wisconsin’,‘Wyoming’),

Please Select <?php foreach($currentState as $state) { ?> <?php echo $state; ?> <?php } ?>

[/php]

How can i assign the value as a number of its position in the array for example Alabama would have a value of 1 and florida would be 10 as alabama is first in the array and florida is 10th

Why don’t you save yourself a lot of time by just doing something like this?

http://www.allembru.com/blog/us-states-dropdown-list-php-function/

It beats trying to reinvent the wheel? 8)

Sponsor our Newsletter | Privacy Policy | Terms of Service