Php 在选项中转换数组
我需要将数组转换为以下格式:Php 在选项中转换数组,php,arrays,Php,Arrays,我需要将数组转换为以下格式: $arrList = Array ( [0] => First Top element [1] => Second Top element [2] => Third Top element [Fourth Top element] => Array ( [0] => Fourth Top element - A [1] => Fourth Top e
$arrList = Array
(
[0] => First Top element
[1] => Second Top element
[2] => Third Top element
[Fourth Top element] => Array
(
[0] => Fourth Top element - A
[1] => Fourth Top element - B
)
[3] => First Bottom element
[4] => Second Bottom element
[5] => Third Bottom element
[Fourth Bottom element] => Array
(
[0] => Fourth Bottom element - A
[1] => Fourth Bottom element - B
)
)
在我的列表的选项中,如下所示:
<option class="red">First Top element</option>
<option class="red">Second Top element</option>
<option class="red">Third Top element</option>
<optgroup label="Fourth Top element" class="red">
<option>Fourth Top element - A</option>
<option>Fourth Top element - B</option>
</optgroup>
<option class="green">First Bottom element</option>
<option class="green">Second Bottom element</option>
<option class="green">Third Bottom element</option>
<optgroup label="Fourth Bottom element" class="green">
<option>Fourth Bottom element - A</option>
<option>Fourth Bottom element - B</option>
</optgroup>
第一个顶部元素
第二顶元素
第三顶元素
第四个顶级元素-A
第四顶元素-B
第一底层元素
第二底层元素
第三底层元素
第四个底部元素-A
第四个底部元素-B
所以我写了这段代码:
foreach ($arrList as $element => $subelement) {
$strClass = '';
if(stristr('top',$subelement) || stristr('top',$element)) {
$strClass = 'class="red"';
} else if(stristr('bottom',$subelement) || stristr('bottom',$element)) {
$strClass = 'class="green"';
}
if(!is_array($subelement)) {
echo '<option '.$strClass.'>' . $subelement . '</option>'."\n";
} else {
echo '<optgroup label="'.$element.'" '.$strClass.'>'."\n";
foreach ($subelement as $elementLetter) {
echo '<option>' . $elementLetter . '</option>'."\n";
}
echo '</optgroup>'."\n";
}
}
foreach($arrlistas$element=>$subelement){
$strClass='';
if(stristr('top',$subelement)| stristr('top',$element)){
$strClass='class=“red”';
}else if(stristr('bottom',$subelement)| stristr('bottom',$element)){
$strClass='class=“绿色”';
}
如果(!is_数组($subelement)){
回显'.$subelement'.''。“\n”;
}否则{
回显“”。“\n”;
foreach($element作为$elementLetter的子元素){
回显'.$elementLetter.'.'.\n';
}
回显“”。“\n”;
}
}
但它写道:
<option >First Top element</option>
<option >Second Top element</option>
<option >Third Top element</option>
<optgroup label="Fourth Top element" class="red">
<option>Fourth Top element - A</option>
<option>Fourth Top element - B</option>
</optgroup>
<option >First Bottom element</option>
<option >Second Bottom element</option>
<option >Third Bottom element</option>
<optgroup label="Fourth Bottom element" class="green">
<option>Fourth Bottom element - A</option>
<option>Fourth Bottom element - B</option>
</optgroup>
第一个顶部元素
第二顶元素
第三顶元素
第四个顶级元素-A
第四顶元素-B
第一底层元素
第二底层元素
第三底层元素
第四个底部元素-A
第四个底部元素-B
有人能帮我吗?所以你基本上是在找这样的东西。我不使用stristr的原因是,它返回字符串中您要查找的部分之后的部分。在这种情况下,你不想要它。那么,为什么要强迫解析器复制字符串,将其分成若干部分,然后将其返回给您呢。。。让你只是。。。连看都不看就把它扔掉。相反,使用strpos更容易,它返回找到字符串的位置。如果没有找到它,它将返回
false
。因此,快速检查“不是假的”就足够了
如果确实需要不区分大小写的检查,可以使用stripos
$list = array();
foreach($arrList as $key=>$el) {
$class = '';
if(strpos($el, 'top') !== false) {
// top class
$class = 'red';
} else {
// if not a top class, then it's bottom apparently
$class = 'green';
}
if(is_array($el)) {
if(strpos($key, 'top') !== false) {
$class = 'red';
} else {
$class = 'green';
}
$list[] = '<optgroup label="'.$key.'" class="'.$class.'">';
foreach($el as $subel) {
$list[] = '<option>'.$subel.'</option>';
}
$list[] = '</optgroup>';
} else {
$list[] = '<option class="'.$class.'">'.$el.'</option>';
}
}
$result = implode("\n", $list);
$list=array();
foreach($arrlistas$key=>$el){
$class='';
if(strpos($el,'top')!==false){
//一流的
$class='red';
}否则{
//如果不是一流的,那么显然是最差的
$class='green';
}
if(is_数组($el)){
if(strpos($key,'top')!==false){
$class='red';
}否则{
$class='green';
}
$list[]='';
外汇($el作为$subel){
$list[]='.$subel';
}
$list[]='';
}否则{
$list[]=''.$el';
}
}
$result=内爆(“\n”,$list);
那么你的代码除了正确的类之外还能工作吗?$strClass
是'
并且你的if语句没有正确触发。您可能需要使用stristr('top',$subelement,true)
来获取top之前的内容,因为top之后没有内容,即使这样,我也不确定它是否有效,因为有空格。