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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c