How to Populate Dropdown List with Array Values in PHP? Last Updated : 24 Apr, 2024 Comments Improve Suggest changes Like Article Like Report We will create an array, and then populate the array elements to the dropdown list in PHP. It is a common task when you want to provide users with a selection of options. There are three approaches to achieve this, including using a foreach loop, array_map() function, and implode() function. Here, we will explore each approach with detailed explanations and code examples. Table of Content Using foreach LoopUsing array_map() FunctionUsing implode() FunctionApproach 1: Populate Dropdown List with Array Values using foreach LoopYou can use a foreach loop to iterate over an array of elements and populate the dropdown list with its values. PHP <?php $options = array( "HTML", "CSS", "JavaScript", "PHP" ); ?> <select> <?php foreach ($options as $option): ?> <option value="<?php echo $option; ?>"> <?php echo $option; ?> </option> <?php endforeach; ?> </select> Output: Approach 2: Populate Dropdown List with Array Values using array_map() FunctionThe array_map() function can be used to apply a callback function to each element of an array. You can use it to generate the <option> tags for the dropdown list. PHP <?php $options = array( "HTML", "CSS", "JavaScript", "PHP" ); ?> <select> <?php echo implode(array_map(function($option) { return "<option value=\"$option\">$option</option>"; }, $options)); ?> </select> Output: Approach 3: Populate Dropdown List with Array Values using implode() FunctionYou can also use the implode() function to concatenate the array values into a string and then use it to populate the dropdown list. PHP <?php $options = array( "HTML", "CSS", "JavaScript", "PHP" ); ?> <select> <?php echo "<option>" . implode("</option><option>", $options) . "</option>"; ?> </select> Output: Comment More infoAdvertise with us Next Article How to Populate Dropdown List with Array Values in PHP? B blalverma92 Follow Improve Article Tags : PHP PHP-Questions Similar Reads How to display array structure and values in PHP ? In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions. It includes Array sizeArray valuesArray value with IndexEach value data type We will display the array structure using 2 min read How to Create Dropdown List in Google Sheets Adding a dropdown list in Google Sheets can make your spreadsheets more dynamic and user-friendly. This feature allows you to create a list of options that users can select from, making data entry quicker and reducing errors. Whether you're managing a budget, tracking projects, or organizing data, d 9 min read How to Get Selected Value in Dropdown List using JavaScript? A dropdown list is an HTML element that allows users to select one option from a list of options. The <select> and <option> tags are used to create dropdown lists in HTML. In this article, we will see how to get selected value from dropdown list using JavaScript.Basic HTML Structure for 2 min read How to generate a drop down list of timezone using PHP ? The timezone_identifiers_list() function is used to generate a dropdown list of timezone with PHP. This function is used to return an indexed array containing all the timezone identifiers. The datetimezone object is sent as a parameter to the timezone_identifiers_list() function and it returns an in 3 min read How to Encode Array in JSON PHP ? Encoding arrays into JSON format is a common task in PHP, especially when building APIs or handling AJAX requests. Below are the approaches to encode arrays into JSON using PHP: Table of Content Using json_encode()Encoding Associative ArraysCustom JSON SerializationUsing json_encode()PHP provides a 2 min read Like