Two arrays in foreach loop

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

    Walk it out...

    $codes = array('tn','us','fr');
    $names = array('Tunisia','United States','France');
    
    • PHP 5.3+

      array_walk($codes, function ($code,$key) use ($names) { 
          echo '';
      });
      
    • Before PHP 5.3

      array_walk($codes, function ($code,$key,$names){ 
          echo '';
      },$names);
      
    • or combine

      array_walk(array_combine($codes,$names), function ($name,$code){ 
          echo '';
      })
      
    • in select

      array_walk(array_combine($codes,$names), function ($name,$code){ 
          @$opts = '';
      })
      echo "";
      

    demo

提交回复
热议问题