Two arrays in foreach loop

后端 未结 22 1568
不思量自难忘°
不思量自难忘° 2020-11-22 04:48

I want to generate a selectbox using two arrays, one containing the country codes and another containing the country names.

This is an example:

22条回答
  •  -上瘾入骨i
    2020-11-22 05:46

    Use an associative array:

    $code_names = array(
                        'tn' => 'Tunisia',
                        'us' => 'United States',
                        'fr' => 'France');
    
    foreach($code_names as $code => $name) {
       //...
    }
    

    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 use array_keys() or array_values() to get exactly the same array you had before.

提交回复
热议问题