thecodedeveloper.com
thecodedeveloper.com

Reputation: 3240

Show array output in select box in php

function merge_common_keys(){
    $arr = func_get_args();
    $num = func_num_args();

    $keys = array();
    $i = 0;
    for($i=0;$i<$num;++$i){
        $keys = array_merge($keys, array_keys($arr[$i]));
    }
    $keys = array_unique($keys);

    $merged = array();

    foreach($keys as $key){
        $merged[$key] = array();
        for($i=0;$i<$num;++$i){
        $merged[$key][] = isset($arr[$i][$key])?$arr[$i][$key]:null;
        }
    }
    return $merged;
    }

$merged = merge_common_keys($find_parent_category,$find_sub_category);
    print_r($merged);

My Output like below

        Array
    (
        [1] => Array
            (
                [0] => Accounting & Financial
                [1] => Array
                    (
                        [Accounting Audit & Assurance] => 24
                        [Accounting Services] => 25
                        [Accounting Software] => 26
                    )

            )

        [6] => Array
            (
                [0] => Creative
                [1] => 
            )

        [7] => Array
            (
                [0] => Data Management
                [1] => Array
                    (
                        [Analytical Tools] => 27
                        [Cloud Computing] => 28
                        [Data Bases] => 29
                    )

            )

    )   

i want show data like that

<select>
<option class="add class" value="">Accounting & Financial</option>
<option value="24">Accounting Audit & Assurance</option>
<option value="25">Accounting Services</option>
<option value="26">Accounting Software</option>
</select>



<select>
<option class="add class" value="">Data Management</option>
<option value="">Analytical Tools</option>
<option value="">Cloud Computing</option>
<option value="">Data Bases</option>
</select>   

Upvotes: 0

Views: 146

Answers (3)

rccoros
rccoros

Reputation: 590

Try this.

foreach ($merged as $select) {
    if(is_array($select[1])){
        echo '<select>';
        echo '<option class="add class" value="">' . $select[0] . '</option>';
        foreach ($select[1] as $opt => $optVal) {
            echo '<option value="'. $optVal .'">' . $opt .'</option>';
        }
        echo '</select>';
    }
}

Upvotes: 1

krishna
krishna

Reputation: 4099

try this

for($i=0;i<sizeof($merged);$i++)
{

echo "<select>";
for($j=0;$j<sizeof($merged[$i]);$j++)
{
if($j == 0)
echo "<option class='add class' value='$merged[$i][$j]'></option>";
else
echo "<option value='$merged[$i][$j]'>$merged[$i][$j]</option>";
}
echo "</select>"

}

Upvotes: 0

Anthony Garcia-Labiad
Anthony Garcia-Labiad

Reputation: 3711

You can try something like

foreach ($merged as $select) {
  echo '<select>';
  echo '<option class="add class" value="">' . $select[0] . '</option>';
  foreach ($select[1] as $opt) {
     echo '<option value="">' . $opt .'</option>';
  }
  echo '</select>';
}

Basically it's just iterating and print the information you want

Upvotes: 1

Related Questions