douchuanghan1344 2015-01-16 07:08
浏览 24
已采纳

字符串到数组然后foreach

I have a variable string which is passed to an and array then foreach that just does not want to work. Below is the code im using.

$explode = $_obj->getModDependencies();
//this variable will returns/echos the string as @ModA,@Mod_b,@Mod3 etc (yes @ is in each value) 

and the foreach and array php code im using

$arr = array($explode);
foreach ($arr as $value) {
echo '<a href="'.$this->getUrl().'mod?mod_id='.$value.'">'.$value.'</a>';
}

if i use the above code it echos one hyperlink with each value at the end of it (http://myurl/mod?mod_id=@ModA,@Mod_b,@Mod3) but i want to echo each hyperlink for each value.

Which would be

http://myurl/mod?mod_id=@ModA

http://myurl/mod?mod_id=@Mod_b

and so forth.

But if i place the actual variable output string directly into the array it echos out how i want it (see below code that works)

$arr = array(@ModA,@Mod_b,@Mod3);
foreach ($arr as $value) {
echo '<a href="'.$this->getUrl().'mod?mod_id='.$value.'">'.$value.'</a>';
}

Any help wold be awesome!!

  • 写回答

2条回答 默认 最新

  • duanqi5333 2015-01-16 07:13
    关注
    $arr = array($explode);
    

    Thats the issue, just by saying something is within array() doesnt really make it an array as you expect it to be. You only gave it 1 argument.

    You also mentioned the value of $explode is like this @ModA,@Mod_b,@Mod3. Just by naming something $explode doesn't explode it. You have to explode it yourself

    $arr=explode(",","@ModA,@Mod_b,@Mod3"); 
    //$arr=explode(",",$explode) in your case
    

    Once that is done, your loop is already fine

    foreach ($arr as $value) {
    echo '<a href="'.$this->getUrl().'mod?mod_id='.$value.'">'.$value.'</a>';
    }
    

    Fiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里