0
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>   
2
  • what did you tried so far? Commented Feb 5, 2014 at 8:17
  • @AnthonyGarcia i m not getting should i changed in function or make diffrent loop? Commented Feb 5, 2014 at 8:18

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Code is working fine but having issue with that one select box <select> <option class="add class" value="">Creative</option> </select> giving error Invalid argument supplied for foreach()
1

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>';
    }
}

4 Comments

code is working fine wht i want but giving that error: <option class="add class" value="">Advertising</option> Warning (2): Invalid argument supplied for foreach()
I don't get that error using the given array. Can you post the whole array that you are trying to show?
how i can remove that option <select> <option class="add class" value="">Creative</option> </select> i mean to say if have single option value that wont show, see my update question
@vikastyagi where you able to try my updated answer?
0

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>"

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.