自动从数组中填充<选择>框

时间:2019-03-18 09:02:35

标签: php html select

我将如何使用以下php值自动填充选择框? 数组(   0 =>   数组(     'slug'=>'acura',     '名称'=>'Ac歌',     'name_en'=>'Ac歌',   ),   1 =>   数组(     'slug'=>'alfa-romeo',     '名称'=>'阿尔法罗密欧',     'name_en'=>'阿尔法罗密欧',   ),   2 =>   数组(     '子弹'=>'高山',     '名称'=>'高山',     'name_en'=>'高山',   ),   3 =>   数组(     'slug'=>'aro',     '名称'=>'ARO',     'name_en'=>'ARO',   ),   4 =>   数组(     'slug'=>'aston-martin',     '名称'=>'阿斯顿·马丁',     'name_en'=>'阿斯顿·马丁',   ),   它被解码的json数据转换为适当的PHP类型。   我只想要'name_en'

3 个答案:

答案 0 :(得分:2)

使用name中的arrayselect选项上使用它

<?php
$arr = array (

  array (
    'slug' => 'acura',
    'name' => 'Acura',
    'name_en' => 'Acura',
  ),
  array (
    'slug' => 'alfa-romeo',
    'name' => 'Alfa Romeo',
    'name_en' => 'Alfa Romeo',
  ),

  array (
    'slug' => 'alpine',
    'name' => 'Alpine',
    'name_en' => 'Alpine',
  ),

  array (
    'slug' => 'aro',
    'name' => 'ARO',
    'name_en' => 'ARO',
  ),

  array (
    'slug' => 'aston-martin',
    'name' => 'Aston Martin',
    'name_en' => 'Aston Martin',
  ),
);   
?>

<select name="names">
<option value="">Select</option>
<?php
foreach($arr as $key => $value):
echo '<option value="'.$key.'">'.$value['name'].'</option>'; 
endforeach;
?>
</select>

答案 1 :(得分:1)

$response = array (
  0 => 
  array (
    'slug' => 'acura',
    'name' => 'Acura',
    'name_en' => 'Acura',
  ),
  1 => 
  array (
    'slug' => 'alfa-romeo',
    'name' => 'Alfa Romeo',
    'name_en' => 'Alfa Romeo',
  ),
  2 => 
  array (
    'slug' => 'alpine',
    'name' => 'Alpine',
    'name_en' => 'Alpine',
  ),
  3 => 
  array (
    'slug' => 'aro',
    'name' => 'ARO',
    'name_en' => 'ARO',
  ),
  4 => 
  array (
    'slug' => 'aston-martin',
    'name' => 'Aston Martin',
    'name_en' => 'Aston Martin',
  )
);
echo "<select>";
foreach($response as $option){
 echo "<options value='".$option['name_en']."'>".$option['name_en']."</options>";
}
echo "</select>";

答案 2 :(得分:0)

您可以尝试以下代码:

<?php
$list = array (
  0 => 
  array (
    'slug' => 'acura',
    'name' => 'Acura',
    'name_en' => 'Acura',
  ),
  1 => 
  array (
    'slug' => 'alfa-romeo',
    'name' => 'Alfa Romeo',
    'name_en' => 'Alfa Romeo',
  ),
  2 => 
  array (
    'slug' => 'alpine',
    'name' => 'Alpine',
    'name_en' => 'Alpine',
  ));
?>
<html>
    <head> </head>
    <body>
        <select>
            <?php foreach ($list as $lv){ ?>
            <option><?= $lv['name'] ?></option>
            <?php } ?>
        </select>
    </body>
</html>

如果您要更改它,我已经列出了name,然后使用另一个索引slugname_en