问题描述
我有一个包含要创建4个< optgroup></optgroup>
的值的数组,意味着每个< optgroup></optgroup>
通过使用PHP foreach
循环
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
我想从
1 Col/1行(lg)
至 12 Col/1行(lg)
选项组标签nam是'Large devices'
1 Col/1 row(lg)
to 12 Col/1 row(lg)
option group lable nam is 'Large devices'
1 Col/1行(md)
到 12 Col/1行(md)
选项组标签nam是'Medium devices'
1 Col/1 row(md)
to 12 Col/1 row(md)
option group lable nam is 'Medium devices'
1 Col/1行(sm)
到 12 Col/1行(sm)
选项组标签nam是'Small devices'
1 Col/1 row(sm)
to 12 Col/1 row(sm)
option group lable nam is 'Small devices'
1 Col/1行(xs)
到 12 Col/1行(xs)
选项组标签nam是'Extra 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 );
推荐答案
根据您给出的示例,我根据您的关键字划分了选项组,首先我使用strpos来获得它们的位置:lg,md,sm和XS,然后我根据您的示例创建了选项组的名称.创建新数组并将每个值分组到指定的组后,我开始迭代选项组并选择.检查以下内容:
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;
这篇关于PHP根据数组的值创建optgroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!