请教一个 PHP 数组填充的算法 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
54dev
V2EX    PHP

请教一个 PHP 数组填充的算法

  •  
  •   54dev 2020-04-19 06:07:51 +08:00 3298 次点击
    这是一个创建于 2002 天前的主题,其中的信息可能已经有所发展或是发生改变。

    条件,已知三个数组

    数组 1 中有固定值

    数组 2,数组 3 长度不定,
    把数组 2,数组 3 的数据填充到数组 1 来生成一个长度等于max(数组 2,数组 3)新的数组

    例如

    $arr1 = ['bot','bot2','','']; $arr2 = ['size1','size2']; $arr3 = ['a','b','c']; 

    生成的新数组是:

    array( 0 => array('bot,'bot2','size1','a'), 1 => array('bot','','size2','b'), 2 => array('bot','','','c'), ); 
    第 1 条附言    2020-04-19 07:54:45 +08:00

    原数据

     $tmp_arr = array('bot1','bot2','bot3','bot4','bot5','bot6','bot7','','','bot10','bot11','bot12','bot13','bot14','bot15','bot16','bot17','bot18', '','','bot21'); $arr1=array('size1','size2'); $arr2=array('a','b','c','d'); 

    填充后数组

    $result = array( array('bot1','bot2','bot3','bot4','bot5','bot6','bot7','1','size1','bot10','bot11','bot12','bot13','bot14','bot15','bot16','bot17','bot18', 'a','1','bot21'), array('bot1','bot2','bot3','bot4','bot5','bot6','bot7','2','size2','bot10','bot11','bot12','bot13','bot14','bot15','bot16','bot17','bot18', 'b','2','bot21'), array('bot1','bot2','bot3','bot4','bot5','bot6','bot7','','','bot10','bot11','bot12','bot13','bot14','bot15','bot16','bot17','bot18', 'c','3','bot21'), array('bot1','bot2','bot3','bot4','bot5','bot6','bot7','','','bot10','bot11','bot12','bot13','bot14','bot15','bot16','bot17','bot18', 'd','4','bot21'), ); 
    4 条回复    2020-04-19 08:00:26 +08:00
    ysc3839
        1
    ysc3839  
       2020-04-19 07:01:38 +08:00   1
    ```
    function myfunc($arr1, $arr2, $arr3) {
    $first = array_shift($arr1);
    return array_map(function(...$arr) use ($first) {
    foreach ($arr as &$value) {
    if ($value == null) $value = '';
    }
    array_unshift($arr, $first);
    return $arr;
    }, $arr1, $arr2, $arr3);
    }

    $arr1 = ['bot','bot2','',''];
    $arr2 = ['size1','size2'];
    $arr3 = ['a','b','c'];

    var_dump(myfunc($arr1, $arr2, $arr3));
    ```

    result:
    array(3) {
    [0]=>array(4) {
    [0]=>string(3) "bot"
    [1]=>string(4) "bot2"
    [2]=>string(5) "size1"
    [3]=>string(1) "a"
    }
    [1]=>array(4) {
    [0]=>string(3) "bot"
    [1]=>string(0) ""
    [2]=>string(5) "size2"
    [3]=>string(1) "b"
    }
    [2]=>array(4) {
    [0]=>string(3) "bot"
    [1]=>string(0) ""
    [2]=>string(0) ""
    [3]=>string(1) "c"
    }
    }
    54dev
        2
    54dev  
    OP
       2020-04-19 07:35:33 +08:00
    @ysc3839 非常感谢,能说一下思路吗,这个闭包没看懂:)
    ysc3839
        3
    ysc3839  
       2020-04-19 07:44:40 +08:00   1
    根据你给的输入和输出,猜测是把 $arr1[0] 去除,然后 $arr1-3 看作矩阵的行进行转置,再把 $arr1[0] 插入第一列。
    把数组第一个元素去掉可以使用 array_shift 。
    矩阵转置可以使用两层循环,但是经过搜索发现使用 array_map 也可以实现转置,而且会自动补齐缺失的元素。但是自动补的是 null,所以预先处理一下,把 null 替换成 '',最后把 $arr1[0] 插入到开头即可。
    54dev
        4
    54dev  
    OP
       2020-04-19 08:00:26 +08:00
    @ysc3839 谢谢,您的思路是对的。我给的例子不太清楚,数组元素很多,还要用的递归和自增,应该需要使用两层循环
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     980 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 23:13 PVG 07:13 LAX 16:13 JFK 19:13
    Do have faith in what you're doing.
    ubao 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