How to populate dropdown list with array values in PHP

Use the PHP foreach loop.
You can simply use the PHP foreach loop to create or populate HTML <select> box or any dropdown menu form the values of an array. Let’s try out an example and see how it works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dropdowns, Combobox, Select, carregar dados Array em PHP</title>
</head>
<body>
<form>
    <select>
        <option selected="selected">Selecione uma opção</option>
        <?php
        // A sample product array
        $products = array("Galaxyz", "Autoracer", "Amorashop", "Kingsman");
        
        // Iterating through the product array
        foreach($products as $item){
        ?>
        <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>
        <?php
        }
        ?>
    </select>
    <input type="submit" value="Submit">
</form>
</body>
</html>
Please follow and like us:
Pin Share

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

RSS
Follow by Email
WP Radio
WP Radio
OFFLINE LIVE