在选项中转换数组

时间:2014-03-27 20:49:41

标签: 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 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>

所以我写了这段代码:

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

但它写道:

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

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

所以你基本上都在寻找这样的东西。我不使用stristr的原因是因为它返回了你正在寻找的部分之后的字符串部分。在这种情况下,你不想要它。那么为什么要强制解析器制作一个字符串的副本,将它分成几部分,然后把它还给你......让你只是......把它扔掉而不用看它。相反,它更容易使用strpos,它返回找到字符串的位置。如果找不到,则返回false。因此快速检查&#34;并非虚假&#34;就足够了。

如果您确实需要案例不敏感检查,可以使用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);