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 做过TCL海信电视小米电视相关影视会员软件私我
  • ¥15 Mapreduce是正常的,在运行其他jar包时并没有任何问题,只是在做LogCount.jar 时出的问题。如图所示
  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题