-3

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?

Thanks

vee
  • 38,255
  • 7
  • 74
  • 78
KHONG sothorn
  • 25
  • 1
  • 1
  • 2
  • 4
    So you don't even take the time to search on this site before asking? http://stackoverflow.com/questions/4480803/two-arrays-in-foreach-loop –  Aug 07 '13 at 05:22
  • 4
    How is it that you're copy and paste reposting the exact same question?! – deceze Aug 07 '13 at 05:27

8 Answers8

5

It has to be 2 arrays? is more easy if you just use an associative array.

like:

$countries = array('tn'=> "Tunisia", "us"=>'United States',"fr"=>'France');
foreach($countries as $code=>$name) {
     echo '<option value="' . $code . '">' . $name . '</option>';    
}

if you already have those two arrays, you can combine them:

$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
$countries = array_combine($codes, $names);

Here is a demo: http://codepad.org/nYjCk3uQ

Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
chris-l
  • 2,802
  • 1
  • 18
  • 18
2

if you are same size of array you can use like below

<?php

$codes = array('tn','us','fr');

$names = array('Tunisia','United States','France');
$i=0
foreach( $names as $name ) {

echo '<option value="' . $codes [$i] . '">' . $name . '</option>';
  $i = $i+1    
}

?>
liyakat
  • 11,825
  • 2
  • 40
  • 46
2

if both arrays are perfectly indexed then you can iterate one array alone

for($i=0; $i<count($codes);$i++) {

echo '<option value="' . $code[$i] . '">' . $name[$i] . '</option>';

}

This will only work if both the arrays are having matching indexes on their values

San
  • 632
  • 4
  • 13
2

All fully tested

3 ways to create a dynamic dropdown from an array.

This will create a dropdown menu from an array and automatically assign its respective value.

Method #1 (Normal Array)

<?php

$names = array('tn'=>'Tunisia','us'=>'United States','fr'=>'France');

echo '<select name="countries">';

foreach($names AS $let=>$word){
    echo '<option value="'.$let.'">'.$word.'</option>';
}
echo '</select>';
 
?>


Method #2 (Normal Array)

<select name="countries">

<?php

$countries = array('tn'=> "Tunisia", "us"=>'United States',"fr"=>'France');
foreach($countries as $select=>$country_name){
echo '<option value="' . $select . '">' . $country_name . '</option>';
}
?>

</select>


Method #3 (Associative Array)

<?php

$my_array = array(
     'tn' => 'Tunisia',
     'us' => 'United States',
     'fr' => 'France'
);

echo '<select name="countries">';
echo '<option value="none">Select...</option>';
foreach ($my_array as $k => $v) {
    echo '<option value="' . $k . '">' . $v . '</option>';
}
echo '</select>';
?>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

Use the for loop for two arrays:

$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
$i = 0;
$length = count($codes);
for($i;$i<$length;$i++) {
    echo '<option value="' . $code[$i] . '">' . $name[$i] . '</option>';
}

This will work for you.

Another ways is here:

$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
$newarr = array_combine($codes,$names);
foreach($newarr as $index => $value){
    echo '<option value="' . $index . '">' . $value . '</option>';
}
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
1
foreach( $codes as $index => $code ) {
   echo '<option value="' . $code . '">' . $names[$index] . '</option>';
}
Prasannjit
  • 795
  • 4
  • 11
0

I would approach it like:

$names = array('Tunisia' : 'tn','United States' :'us','France': 'fr');
foreach($names as $key=>$value) {
echo '<option value="' . $value . '">' . $key . '</option>';
}

Another way can be:

$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
$count=0
foreach( $names as $name ) {
echo '<option value="' . $codes [$i] . '">' . $name . '</option>';
  $count = $count + 1;  
}
Engineer
  • 5,911
  • 4
  • 31
  • 58
0

Try this

 foreach( $codes as $index => $code ) {
    echo '<option value="' . $code . '">' . $names[$index] . '</option>';
     }
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75