0

I have an array contains 24 values I want to create 4 <optgroup></optgroup>, means every <optgroup></optgroup> contains six values within select box by using PHP foreach loop

I want from

1 Col/1 row(lg) to 12 Col/1 row(lg) option group lable nam is 'Large devices'

1 Col/1 row(md) to 12 Col/1 row(md) option group lable nam is 'Medium devices'

1 Col/1 row(sm) to 12 Col/1 row(sm) option group lable nam is 'Small devices'

1 Col/1 row(xs) to 12 Col/1 row(xs) option group lable nam is 'Extra small devices'

$YPE_grid = array( '1 Col/1 row(lg)' => 12.1, '2 Col/1 row(lg)' => 6.2, '3 Col/1 row(lg)' => 4.3, '4 Col/1 row(lg)' => 3.4, '6 Col/1 row(lg)' => 2.5, '12 Col/1 row(lg)' => 1.6 , '1 Col/1 row(md)' => 12.7, '2 Col/1 row(md)' => 6.8, '3 Col/1 row(md)' => 4.9, '4 Col/1 row(md)' => 3.10, '6 Col/1 row(md)' => 2.11, '12 Col/1 row(md)' => 1.12, '1 Col/1 row(sm)' => 12.13, '2 Col/1 row(sm)' => 6.14, '3 Col/1 row(sm)' => 4.15, '4 Col/1 row(sm)' => 3.16, '6 Col/1 row(sm)' => 2.17, '12 Col/1 row(sm)' => 1.18, '1 Col/1 row(xs)' => 12.19, '2 Col/1 row(xs)' => 6.21, '3 Col/1 row(xs)' => 4.22, '4 Col/1 row(xs)' => 3.23, '6 Col/1 row(xs)' => 2.24, '12 Col/1 row(xs)' => 1.25 );
5
  • Sure, should we do it with any framework or just PHP is fine for you? Should we use bootstrap for the select? Commented Nov 24, 2016 at 13:12
  • i want do this by using foreach php loop Commented Nov 24, 2016 at 13:14
  • And what is stopping you? Commented Nov 24, 2016 at 13:16
  • @u_mulder I know this is may do easy for PHP but I can't do this if you can please help me. I want to create four option groups for array dynamically within the foreach loop Commented Nov 24, 2016 at 13:19
  • 3
    he is not saying that your problem is easy or difficult to implement, we are not here to judge your skills, this is a place to learn and improve. But you should show something, even if it is wrong or crazy, show some effort, SO is not a code writing service. Commented Nov 24, 2016 at 13:29

1 Answer 1

1

Based on your given example, I divided the option group based on your keywords, first i used the strpos to get the position of these: lg, md, sm and xs, then i created the name of the option group based on your example. After I created the new array and group each of those values on their designated groups, I started iterating the option group and select. Check this:

$YPE_grid = array( '1 Col/1 row(lg)' => 12.1, '2 Col/1 row(lg)' => 6.2, '3 Col/1 row(lg)' => 4.3, '4 Col/1 row(lg)' => 3.4, '6 Col/1 row(lg)' => 2.5, '12 Col/1 row(lg)' => 1.6 , '1 Col/1 row(md)' => 12.7, '2 Col/1 row(md)' => 6.8, '3 Col/1 row(md)' => 4.9, '4 Col/1 row(md)' => 3.10, '6 Col/1 row(md)' => 2.11, '12 Col/1 row(md)' => 1.12, '1 Col/1 row(sm)' => 12.13, '2 Col/1 row(sm)' => 6.14, '3 Col/1 row(sm)' => 4.15, '4 Col/1 row(sm)' => 3.16, '6 Col/1 row(sm)' => 2.17, '12 Col/1 row(sm)' => 1.18, '1 Col/1 row(xs)' => 12.19, '2 Col/1 row(xs)' => 6.21, '3 Col/1 row(xs)' => 4.22, '4 Col/1 row(xs)' => 3.23, '6 Col/1 row(xs)' => 2.24, '12 Col/1 row(xs)' => 1.25 );

    $newArray = array();
    foreach($YPE_grid as $key => $value) {
        if(strpos($key,"lg") > 0) {
            $newArray['"Large Devices"'][$key] = $value;
        } else if(strpos($key,"md") > 0) {
            $newArray['"Medium devices"'][$key] = $value;
        } else if(strpos($key,"sm") > 0) {
            $newArray['"Small devices"'][$key] = $value;
        } else if(strpos($key,"xs") > 0) {
            $newArray['"Extra small devices"'][$key] = $value;
        }
     }

$select = "";
$select .= "<select>";
foreach($newArray as $key => $value) {
    $select .= "<optgroup label=" . $key . ">";
        if(is_array($value)) {
            foreach($value as $key2 => $value2) {
                $select .= "<option value='" . $key2 . "'>" . $key2 . "(" . $value2 . ")</option>";
            }
        }
    $select .= "</optgroup>";
}

$select .= "</select>";
echo $select;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it is amazing idea for solving my problem

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.