Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this nested array and i want to convert it into dropdown ,but at output it is just showing me combo-box with options as (array ,array,array)

 <select name="pcity" id="pcity" multiple="multiple">

<?php
$pcitylist = array('Andaman and Nicobar' => array('North and Middle Andaman',
    'South Andaman', 'Nicobar'), 'Andhra Pradesh' => array('Adilabad', 'Anantapur',
    'Chittoor', 'East Godavari', 'Guntur', 'Hyderabad', 'Kadapa', 'Karimnagar',
    'Khammam', 'Krishna', 'Kurnool', 'Mahbubnagar', 'Medak', 'Nalgonda', 'Nellore',
    'Nizamabad', 'Prakasam', 'Rangareddi', 'Srikakulam', 'Vishakhapatnam',
    'Vizianagaram', 'Warangal', 'West Godavari'), 'Arunachal Pradesh' => array('Anjaw',
    'Changlang', 'East Kameng', 'Lohit', 'Lower Subansiri', 'Papum Pare', 'Tirap',
    'Dibang Valley', 'Upper Subansiri', 'West Kameng'));
foreach ($pcitylist as $pcitylist1) {
    echo '<option value="' . $pcitylist1 . '"' . (isset($_POST['pcity']) && $_POST['pcity'] ==
        $pcitylist1 ? ' selected' : '') . '>' . $pcitylist1 . '</option>';
}              

 ?>         
 </select>

i want it to display like this

<select>
<optgroup>Andaman and Nicobar</optgroup>
<option>North and Middle Andaman</option>
<option>South Andaman</option>.....
</select>

and so on...

share|improve this question
<select> <optgroup>Andaman and Nicobar</optgroup> <option>Adilabad</option> <option>Anantapur</option> </select> Syntax is not correct. – Pankit Kapadia Dec 14 '12 at 7:18
@PankitKapadia you should show him the correct syntax. – beerwin Dec 14 '12 at 7:37
1  
The correct syntax would be: <select><optgroup label="Andaman And Nicobar"><option>Adilabad</option><option>Anantapur</option><option></option></o‌​ptgroup></select> – beerwin Dec 14 '12 at 7:42
@beerwin - thanks showing him on behalf of me..:) – Pankit Kapadia Dec 14 '12 at 7:42

2 Answers

up vote 1 down vote accepted
foreach ($pcitylist as $key => $pcitylist1)
{
      echo '<optgroup label="'.$key.'">';
      foreach ($pcitylist1 as $finalCity) {
          echo '<option value="' . $finalCity . '"' . (isset($_POST['pcity']) && $_POST['pcity'] == $finalCity ? ' selected' : '') . '>' . $finalCity . '</option>';
      }   
      echo '</optgroup>';
} 

The $key holds the optgroup label. This will work with your array.

share|improve this answer
Thanks a lot it gave me proper output – raj Dec 14 '12 at 9:18
Then please accept this answer. – beerwin Dec 14 '12 at 11:47
1  
I need 15 reputation to accept this answer sorry – raj Dec 17 '12 at 7:53

It is an multidimensional array... use one more for loop inside ur for loop and u will get the ouput..

Try the following..

foreach ($pcitylist as $key => $pcitylist1)
{ 
      foreach ($pcitylist1 as $finalCity) {
          echo '<option value="' . $finalCity . '"' . (isset($_POST['pcity']) && $_POST['pcity'] == $finalCity ? ' selected' : '') . '>'.$key . $finalCity . '</option>';
      }    
} 
share|improve this answer
Thnks for your responce ,with your code I am getting List options only ,i want list options with their group name ,as we get it using <optgroup> tag .Means i want option Group name also to come in that list – raj Dec 14 '12 at 7:26
use this variable inside forloop to access group name.. $pcitylist1 – Vinoth Babu Dec 14 '12 at 7:31
if its working... please accept the answer as right.. so i can get some reput... :) – Vinoth Babu Dec 14 '12 at 7:31
since i am new to php can u write how to use this $pcitylist1 in for loop ,so i can get result – raj Dec 14 '12 at 7:36
edited my code in answer.. use that.. – Vinoth Babu Dec 14 '12 at 7:37
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.