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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示