PHP 无规则数组如何迭代拼接到想要的效果呢? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Bule
V2EX    PHP

PHP 无规则数组如何迭代拼接到想要的效果呢?

  •  
  •   Bule 2018-01-18 18:59:32 +08:00 3116 次点击
    这是一个创建于 2872 天前的主题,其中的信息可能已经有所发展或是发生改变。

    数组:

    $init = [ 'router/aaa' => [ 'opear' => ['1.php', '2.php'], 'list' => ['1.php', '2.php'] ], 'router/bbb' => [ 'url' => ['a.php', 'b.php'] ], 'router/homehelp' => [ 'opear' => [ 'update' => ['1.php', '2.php'], 'delete' => ['1.php', '2.php'] ], 'status' => ['start.php', 'stop.php'] ] ]; 

    效果:

    array( 0 => 'router/aaa/opear/1.php', 1 => 'router/aaa/opear/2.php', 2 => 'router/aaa/list/1.php', 3 => 'router/aaa/list/2.php', 4 => 'router/bbb/url/a.php', 5 => 'router/bbb/url/b.php', 6 => 'router/home/help/opear/update/1.php', 7 => 'router/home/help/opear/update/2.php', ... ); 
    11 条回复    2018-01-19 14:25:35 +08:00
    loginv2
        1
    loginv2  
       2018-01-18 19:14:05 +08:00 via Android
    递归遍历?
    loadinger
        2
    loadinger  
       2018-01-18 19:28:41 +08:00
    递归不就可以么。
    gclove
        3
    gclove  
       2018-01-18 19:32:24 +08:00
    @Bule 简单, 先判断遍历的结果是不是一个数组,是的话调用自己。 否则输出 /保存
    Bule
        4
    Bule  
    OP
       2018-01-19 11:25:37 +08:00
    @gclove 想不通额,请问有 demo 示例吗?
    eoo
        5
    eoo  
       2018-01-19 11:32:07 +08:00 via Android
    <?php
    //无限遍历数组
    $a1 = array("a", "b", "c"); //一维数组
    $a2 = array(array(21, 3, 6), array("a", "b", "c")); //二维数组
    $a3 = array(array(array(5, 55), 4, 444), 2, 7, 6, 8, array("w", "d", array(3, 2, "a"), "s")); //多维不规则数组
    function fun($a) {
    foreach ($a as $val) {
    if (is_array($val)) { //如果键值是数组,则进行函数递归调用
    fun($val);
    } else { // 如果键值是数值,则进行输出
    echo "$val<br />";
    } //end if

    } //end foreach

    } //end fun
    //fun($a1);
    //fun($a2);
    fun($a3);
    ?>
    vincenttone
        6
    vincenttone  
       2018-01-19 11:56:40 +08:00
    试试这个
    ```
    function r($arr, $pre = '')
    {
    $r = [];
    foreach ($arr as $_k => $_v) {
    if (is_array($_v) && !empty($_v)) {
    $r = array_merge($r, r($_v, $pre . '/' . $_k));
    } else {
    $r[] = $pre . '/' . $_v;
    }
    return $r;
    }
    ```
    vincenttone
        7
    vincenttone  
       2018-01-19 11:58:29 +08:00
    顺路打听一下怎么发代码块呢?
    Bule
        8
    Bule  
    OP
       2018-01-19 12:06:26 +08:00
    @vincenttone
    ```语言( php )
    code
    ```
    Bule
        9
    Bule  
    OP
       2018-01-19 12:09:59 +08:00
    @vincenttone 好 6 啊,这方面的知识是多看看数据结构吗?
    dee
        10
    dee  
       2018-01-19 14:16:16 +08:00
    也贴一个 虽然不是迭代
    ```php
    function router($ary, &$newAry, $pre = '')
    {
    foreach($ary as $key => $val)
    {
    if(is_array($val))
    {
    router($val, $newAry, "$pre/$key");
    }
    else
    {
    $newAry[] = "{$pre}/{$val}";
    }
    }
    }
    ```
    vincenttone
        11
    vincenttone  
       2018-01-19 14:25:35 +08:00
    @Bule 我写的方法是一个递归,如果要和数据结构说到一起的话,写过一些树形结构的话,会有一些影响吧。
    但是其实递归也算是一种常用的编程套路,很多时候比循环实现更容易一些吧,所以实践中经常会用到,多写代码也就好了。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2538 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 20ms UTC11:54 PVG 19:54 LAX 03:54 JFK 06:54
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86