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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据数组的数量循环遍历数组和输出链接相关的知识,希望对你有一定的参考价值。

我目前有一个数组如下:

   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 )
 )

我需要做的是最终打印9个链接(因为有9个数组元素),但基于数组中的键。例如:

www.samplelink/link/id/another_id/third_id

但我似乎无法让循环正确。到目前为止我所拥有的是:

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

但这并不是我打印出钥匙所需要的。谁知道我能做什么?

答案

如果子数组中的元素总是以相同的顺序排列,那么你可以只使用implode

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

否则你应该明确指出哪个索引在哪个位置,如@Danyal Sandeelo的答案。

另一答案
 foreach ($array as $innerArray) {
  echo "<a>www.samplelink/link/".$innerArray['id']."/".$innerArray['another_id']."/".$innerArray['third_id']."</a>";
 }

如果密钥不存在,它可以给出undefined index错误,所以你也可以这样做:

foreach ($array as $innerArray) {
    $finalLink = array_key_exists('id',$innerArray)?$innerArray['id']:"";
    $finalLink.= "/".array_key_exists('another_id',$innerArray)?$innerArray['another_id']:"";
    $finalLink.= "/".array_key_exists('third_id',$innerArray)?$innerArray['third_id']:"";
    echo "<a>www.samplelink/link/$finalLink</a>";
}
另一答案
<?php

$arrays = array(
    "0" => array('id' => 30, 'another_id' => 2800, 'third_id' => 1),
    "1" => array('id' => 31, 'another_id' => 2801, 'third_id' => 1),
    "2" => array('id' => 32, 'another_id' => 2802, 'third_id' => 1),
    "3" => array('id' => 33, 'another_id' => 2803, 'third_id' => 1),
    "4" => array('id' => 34, 'another_id' => 2804, 'third_id' => 1),
    "5" => array('id' => 35, 'another_id' => 2805, 'third_id' => 2),
    "6" => array('id' => 36, 'another_id' => 2806, 'third_id' => 3),
    "7" => array('id' => 37, 'another_id' => 2807, 'third_id' => 3),
    "8" => array('id' => 38, 'another_id' => 2808, 'third_id' => 2),
    "9" => array('id' => 39, 'another_id' => 2809, 'third_id' => 2),
);

foreach($arrays as $key => $array) {
   echo 'www.samplelink/link/'.$array['id'].'/'.$array['another_id'].'/'.$array['third_id']. "
";
}

你可以在这里玩http://sandbox.onlinephpfunctions.com/code/1c7838e25045263de03e23c60b19c86d5640407d

另一答案

你可以这样做 :

foreach ($array as $arr) {
    echo "<a>www.samplelink/link/" . $arr['id'] . '/' . $arr['another_id'] . '/' . $arr['third_id'] . "</a>";
}

或者,如果你的子数组总是以正确的顺序保存id,除了相关的那些之外什么都没有:

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

$value将在每次迭代时保持内部数组。

foreach($array as $value) {
  $link = '//www.samplelink/link/'.$value['id'].'/'.$value['another_id'].'/'.$value['third_id']
  echo '<a>'.$link.'</a>';
}

以上是关于根据数组的数量循环遍历数组和输出链接的主要内容,如果未能解决你的问题,请参考以下文章

怎么用一个循环遍历二维数组?

js数组遍历方法总结

Java数组的--遍历

如何禁用使用手表循环遍历数组的可用时间?

for循环二维数组的取值方式?

流程控制和数组