How to get value of Array in foreach loop

While working on my CodeIgniter based Open Source Project Management tool “Koronio”, suddenly I needed create a loop to generate a Dropdown element with array key as option value. This is very simple trick, provably everyone knows it, though I felt to share it.

Lets say, we have a php array named $priority which has 5 items.

$priority=array('Low','Normal Low',"Normal",'Normal High','High');

We want to show this items in a Select Item with Array value as option name and Array key as option value.

foreach($priority as $key=> $val)
echo '<option value="'.$key.'">'.$val.'</option>';

Thats It! You’ll have a dropdown list with the Priority list and value as option value.

Author: Ariful Haque

Software Engineer | Freelance Web Application Developer PHP, CodeIgniter, WordPress, JavaScript, jQuery, Ajax, Ubuntu, CentOS, Apache..

1 thought on “How to get value of Array in foreach loop”

Leave a Reply

Your email address will not be published. Required fields are marked *