Two arrays in foreach loop

后端 未结 22 1571
不思量自难忘°
不思量自难忘° 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:52

    foreach operates on only one array at a time.

    The way your array is structured, you can array_combine() them into an array of key-value pairs then foreach that single array:

    foreach (array_combine($codes, $names) as $code => $name) {
        echo '';
    }
    

    Or as seen in the other answers, you can hardcode an associative array instead.

提交回复
热议问题