duanha3539 2015-12-10 15:52
浏览 68
已采纳

从php数组生成输出

i'm trying to generate a firewall template from a php config

i've got this array

$config=array('1024','3306','3804','127017');

and i would like this output

1024:3305
3307:3803
3805:127017

as you can see, the first and last value stay the same, the value in between are -1 / +1

i think i can do it but not in a clean way, i tried to manipulate the array but in a wrong way i guess because the code is too verbose

may you help ?

thanks

  • 写回答

4条回答 默认 最新

  • dssqq82402 2015-12-10 16:04
    关注
    $config=array('1024','3306','3804','127017');
    
    $output = [];
    
    foreach ($config as $k => $port) {
        if (isset($config[$k+1])) {
            $output[] = ($k ? $port + 1 : $port)
                . ':'
                . (isset($config[$k+2]) ? ($config[$k + 1] - 1) : $config[$k + 1]);
        }
    }
    
    echo implode("<br>", $output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥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设计 源简并电感型共源放大器