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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真