How to populate dropdown list with array values in PHP

  18867 views   1 year ago 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">
<meta charset="utf-8">
<title>Dynamically Generate Select Dropdowns</title>
        <option selected="selected">Choose one</option>
        // A sample product array
        $products = array("Mobile", "Laptop", "Tablet", "Camera");
        // Iterating through the product array
        foreach($products as $item){
        <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>
    <input type="submit" value="Submit">


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]

Related Articles