Two arrays in foreach loop

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

    Few arrays can also be iterated like this:

    foreach($array1 as $key=>$val){ // Loop though one array
        $val2 = $array2[$key]; // Get the values from the other arrays
        $val3 = $array3[$key];
        $result[] = array( //Save result in third array
          'id' => $val,
          'quant' => $val2,
          'name' => $val3,
        );
      }
    

提交回复
热议问题