douhuan1979 2015-10-30 17:04 采纳率: 100%
浏览 51

从2个阵列构建单个下拉菜单

I'm trying to build a drop-down from 2 arrays, and I've got the options working but the nested foreach loop is not working, so each option in the drop down has the same title (first item of array $linkText). What is the correct way of doing this? Thanks.

$content.='<select onChange="window.location.href=this.value">';
      $content.='  <option selected="selected">Choose one</option>';

 foreach($dateLinks as $item1){ 
               $content.='<option value="'.$item1.'">';

               foreach($linkText as $item2){
                 $content.='   '.$item2.'  </option>';
}
}

$content.='</select> ';

The arrays look like this:

$dateLinks = array(
    '/latest/potm/previousmonths/Oct2003',
    '/latest/potm/previousmonths/Nov2003',
    '/latest/potm/previousmonths/Dec2003',
    '/latest/potm/previousmonths/Jan2004'
);

$linkText = array('Oct2003', 'Nov2003', 'Dec2003', 'Jan2004');
  • 写回答

1条回答 默认 最新

  • dongluoqiu0255 2015-10-30 17:59
    关注

    Ok nevermind sorry. I managed to do it with a little more research, by combining the arrays:

     $c = array_combine($linkText, $dateLinks);
    
          foreach($c as $key => $value){ 
                   $content.='<option value="'.$value.'"> '.$key.' </option> ';
                   }
    
         $content.='</select> ';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?