dongtong848825 2013-07-24 08:21
浏览 8
已采纳

如何显示特定的阵列组合?

i want to display following array in output as 246,357 that is donot process first subarray and also the remaining subarray combination should be like $array[1][0].$array[2][0].$array[3][0],similarly combination should be $array[1][1].$array[2][1].$array[3][1]

$array=[
          [0,1],
          [2,3],
          [4,5],
          [6,7]
       ];

i have written follwing code but could not bypass first subarray so my output is 0246,1357.plz help.

foreach($array as $n)
{
    $a.=$n[0];
    $b.=$n[1];
}
echo "$a".","."$b";
  • 写回答

5条回答 默认 最新

  • doukun8944 2013-07-24 08:25
    关注

    One way to skip the first element would be to use a flag variable $first:

    $first = true;
    foreach ($array as $n) {
        if ($first) {
            $first = false;
        } else {
            $a .= $n[0];
            $b .= $n[1];
        }
    }
    

    Another way would be to remove the first element from the array, so it will be skipped over:

    unset($array[0]);
    

    Or check the key from within the foreach loop:

    foreach ($array as $k => $n) {
        if ($k > 0) {
            $a .= $n[0];
            $b .= $n[1];
        }
    }
    

    Yet another way would be to use array_shift(), which changes numeric keys in addition to removing the first element:

    array_shift($array);
    

    Finally, because your array contains only consecutive integer keys starting from 0, you could just use a normal for loop:

    for ($i = 1; $i < count($array); $i++) {
        $a .= $array[$i][0];
        $b .= $array[$i][1];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器