dreamact3026 2016-01-20 12:50
浏览 83
已采纳

在PHP中将值从一个数组连接到另一个数组

I am trying to create a function to generate all the possible combinations of a particular group of characters, and have it variable based on length.

I have a function to create an array of the characters I would like, and this works fine.

function generate_characters($l, $n, $d) {
    $r = array();

    if ($l === true) {
        foreach (range('a', 'z') as $index) {
            array_push($r, $index);
        }
    }

    if ($n === true) { array_push($r, '0','1','2','3','4','5','6','7','8','9'); }


    if ($d === true) { array_push($r, '-'); }

    return $r;
}

I then need to have it create an array of all possible combinations based on $length, for example if '$length = 1' I need the following array

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
    [9] => j
    [10] => k
    [11] => l
    [12] => m
    [13] => n
    [14] => o
    [15] => p
    [.... removed some values to save on length ....]
    [35] => 9
)

but if '$length = 2' I need this

Array
(
    [0] => aa
    [1] => ab
    [2] => ac
    [3] => ad
    [4] => ae
    [5] => af
    [6] => ag
    [7] => ah
    [8] => ai
    [9] => aj
    [.... removed some values to save on length ....]
    [1329] => 97
    [1330] => 98
    [1331] => 99
)

I have tried array_walk() and array_walk_recursive(), along with several foreach and while loops, to no avail.

I can get it to work by manually doing it for each length, but not with a variable length by doing this, but don't know how to make it variable by length.

function generate_two($l, $n, $d) {
    $r = array();

    foreach (generate_characters($l, $n, false) as $v1) {
        foreach (generate_characters($l, $n, $d) as $v2) {
            array_push($results, "$v1$v2");
        }
    }
    return $r;
}

all this whilst, not having the '-' as the first character, although I could remove those values after generating the array if I needed to.

Thanks, Dan

  • 写回答

4条回答 默认 最新

  • dongwu8653 2016-01-20 13:43
    关注

    Presuming you want to use the array you created as the array to use to append to. I can't see why you'd need to work with specific characters other than that in the array (I may be wrong, but this can be easily adapted to cater for that).

    /**
     * @param array $array
     * @param $length
     * @param null $original
     * @return array
     */
    function generate_values(array $array, $length, $original = null) {
    
        // If length is 1 or less just return the array
        if ($length <= 1) {
            return $array;
        }
    
        // The resulting values array
        $result = [];
    
        // Copy the array if original doesn't exist
        if (!is_array($original)) {
            $original = $array;
        }
    
        // Loop over each item and append the original values
        foreach($array as $item) {
            foreach($original as $character) {
                $result[] = $item . $character;
            };
        }
    
        // Recursively generate values until the length is 1
        return generate_values($result, --$length, $original);
    }
    

    To use it you can use your generator.

    $characterArray = generate_characters(true, false, false);
    
    $results = generate_values($characterArray, 2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题