PHP Select optgroup from array

0.47K views
no comments
28 Nov 2015 11:36 pm

I have a array that contains continent and with countries.

Array
(
    [0] => Array
        (
            [continent_name] => asia
            [country_name] => pakistan
            [country_value] => 1
        )

    [1] => Array
        (
            [continent_name] => asia
            [country_name] => india
            [country_value] => 2
        )

    [2] => Array
        (
            [continent_name] => asia
            [country_name] => china
            [country_value] => 3
        )

    [3] => Array
        (
            [continent_name] => asia
            [country_name] => bangladesh
            [country_value] => 4
        )

    [4] => Array
        (
            [continent_name] => antarctica
            [country_name] => 123
            [country_value] => 10
        )

    [5] => Array
        (
            [continent_name] => europe
            [country_name] => canada
            [country_value] => 5
        )

    [6] => Array
        (
            [continent_name] => europe
            [country_name] => germany
            [country_value] => 6
        )

    [7] => Array
        (
            [continent_name] => europe
            [country_name] => sweden
            [country_value] => 7
        )

    [8] => Array
        (
            [continent_name] => asia
            [country_name] => iraq
            [country_value] => 8
        )

    [9] => Array
        (
            [continent_name] => antarctica
            [country_name] => abc
            [country_value] => 8
        )

    [10] => Array
        (
            [continent_name] => antarctica
            [country_name] => xyz
            [country_value] => 9
        )

)

Now we need to reorder this array.

foreach($continent_countries as $array_key=>$array_values) {
	$new_array = $array_values['continent_name'];
	$new_sel_array[$new_array][] = array('c_name' => $array_values['country_name'], 'c_value' => $array_values['country_value']);
}

array has been reorder with continent

Array
(
    [asia] => Array
        (
            [0] => Array
                (
                    [c_name] => pakistan
                    [c_value] => 1
                )

            [1] => Array
                (
                    [c_name] => india
                    [c_value] => 2
                )
            [2] => Array
                (
                    [c_name] => china
                    [c_value] => 3
                )
            [3] => Array
                (
                    [c_name] => bangladesh
                    [c_value] => 4
                )
            [4] => Array
                (
                    [c_name] => iraq
                    [c_value] => 8
                )

        )
    [antarctica] => Array
        (
            [0] => Array
                (
                    [c_name] => 123
                    [c_value] => 10
                )
            [1] => Array
                (
                    [c_name] => abc
                    [c_value] => 8
                )
            [2] => Array
                (
                    [c_name] => xyz
                    [c_value] => 9
                )
        )
    [europe] => Array
        (
            [0] => Array
                (
                    [c_name] => canada
                    [c_value] => 5
                )
            [1] => Array
                (
                    [c_name] => germany
                    [c_value] => 6
                )
            [2] => Array
                (
                    [c_name] => sweden
                    [c_value] => 7
                )
        )
)

now set select with option group

echo '<select>';
foreach($new_sel_array as $sel_opt=>$sel_opt_array){
	echo '<optgroup label="'.$sel_opt.'"></optgroup>';
		$for_limit = count($sel_opt_array);
		for($sel_inc=0; $sel_inc<$for_limit; $sel_inc++) {
			$c_name = $sel_opt_array[$sel_inc]['c_name'];
			$c_value = $sel_opt_array[$sel_inc]['c_value'];
			echo '<option value="'.$c_value.'">'.$c_name.'</option>';
		}
}
echo '</select>';

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>