I want to generate a selectbox
using two arrays, one containing the country codes and another containing the country names.
This is an example:
<?php
$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
foreach( $codes as $code and $names as $name ) {
echo '<option value="' . $code . '">' . $name . '</option>';
}
?>
This method didn't work for me. Any suggestions?
Now you can use $a for each array....
Use an associative array:
I believe that using an associative array is the most sensible approach as opposed to using
array_combine()
because once you have an associative array, you can simply usearray_keys()
orarray_values()
to get exactly the same array you had before.I think that you can do something like:
$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
It should also work for associative arrays.