# Create a drop down menu from an array

**URL:** <https://forum.kirupa.com/t/create-a-drop-down-menu-from-an-array/222344>\
**Category:** Uncategorized\
**Created:** [April 12, 2007, 4:39pm UTC](https://forum.kirupa.com/t/create-a-drop-down-menu-from-an-array/222344 "2007-04-12T16:39:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 12, 2007, 4:39pm UTC](https://forum.kirupa.com/t/create-a-drop-down-menu-from-an-array/222344/1 "2007-04-12T16:39:53Z")

</div>

Hello all

Hope your all well.

I have made(found and edited and such) this code that creates a drop down menu from an array which works great.

```php

// drop down menu function
function generate_drop_menu($name, $options, $default=""){
	if (!empty($options)){
		$html = '<dl><select name="'.$name.'" id="'.$name.'">';
		foreach ($options as $value => $label){
			$html .='<option ';
		if ($value == $default){
			$html .='selected ';
		}
		// $label[] array numbered
		$html .='value="'.$value.'" >'.$label[1].'</option>';
		}
		$html .='</select></dl>';
		return $html;
	}else{
		$error = "Data supplied is not valid<br />";
		return $error;
	}
}

//call the function
echo generate_drop_menu('product', $cms_product, '<br />', '');

```

This code works great with just an array but i’m pulling data out of a database and creating a multidimensional array.

How can i edit this code to create a drop down menu so it can be used and used again no matter what data i put into it. Without getting a list of Array Array Array.

note: This data is being pulled out as an associative array.

This is the array i’m using.

```auto

Array
(
    [0] => Array
        (
            [product_id] => 1
            [product_name] => chairs
        )

    [1] => Array
        (
            [product_id] => 2
            [product_name] => sofa
        )

    [2] => Array
        (
            [product_id] => 3
            [product_name] => tables
        )

    [3] => Array
        (
            [product_id] => 4
            [product_name] => barstools
        )

    [4] => Array
        (
            [product_id] => 5
            [product_name] => benches
        )

    [5] => Array
        (
            [product_id] => 6
            [product_name] => lighting
        )

    [6] => Array
        (
            [product_id] => 7
            [product_name] => chaise
        )

    [7] => Array
        (
            [product_id] => 8
            [product_name] => stool
        )

)

```

I can quite easily create the drop down menu for it using this.

```php

<?php 
foreach ($cms_product as $arr)
	{echo "<option value=\"". $arr['product_id'] . "\">" . $arr['product_name'] . "</option>";}
?>

```

But wanted to create a function to create the list for me as there are quite a few drop down menus.
