Two arrays in foreach loop

后端 未结 22 1897
不思量自难忘°
不思量自难忘° 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条回答
  •  佛祖请我去吃肉
    2020-11-22 05:30

    array_map seems good for this too

    $codes = array('tn','us','fr');
    $names = array('Tunisia','United States','France');
    
    array_map(function ($code, $name) {
        echo '';
    }, $codes, $names);
    

    Other benefits are:

    • If one array is shorter than the other, the callback receive null values to fill in the gap.

    • You can use more than 2 arrays to iterate through.

提交回复
热议问题