dongmu6578 2017-12-19 13:17
浏览 78
已采纳

根据数组的数量循环遍历数组和输出链接

I currently have an array as follows:

   Array ( 
    [0] => Array ( [id] => 34 [another_id] => 2805 [third_id] => 1 ) 
    [1] => Array ( [id] => 35 [another_id] => 2805 [third_id] => 1 ) 
    [2] => Array ( [id] => 36 [another_id] => 2805 [third_id] => 1 ) 
    [3] => Array ( [id] => 37 [another_id] => 2805 [third_id] => 1 ) 
    [4] => Array ( [id] => 38 [another_id] => 2805 [third_id] => 1 ) 
    [5] => Array ( [id] => 39 [another_id] => 2805 [third_id] => 1 ) 
    [6] => Array ( [id] => 40 [another_id] => 2805 [third_id] => 2 ) 
    [7] => Array ( [id] => 41 [another_id] => 2805 [third_id] => 2 )
    [8] => Array ( [id] => 42 [another_id] => 2805 [third_id] => 2 ) 
    [9] => Array ( [id] => 43 [another_id] => 2805 [third_id] => 2 )
 )

What I need to do is ultimately print out 9 links ( as there are 9 array elements) but based on the keys in the array. For example:

www.samplelink/link/id/another_id/third_id

But I can't seem to get the loop right. What I have so far is:

foreach ($array as $arr) {
  foreach ( $arr as $key => $value ) {
    echo "<a>www.samplelink/link/".$key[$value]."</a>";
  }
}

But thats not exactly what I need as its printing out the keys as well. Anyone know what I could do?

  • 写回答

5条回答 默认 最新

  • douzhao6584 2017-12-19 13:20
    关注

    If elements in subarrays always in same order, you can just implode them:

    foreach ($array as $arr) {
        echo "<a>www.samplelink/link/".implode('/', $arr)."</a>";
    }
    

    Otherwise you should point what index will be in which position explicitly, as in @Danyal Sandeelo's answer.

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

报告相同问题?

悬赏问题

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