简体   繁体   English

PHP数组收集

[英]PHP array collect

I have alphabet array 24 character: "ABCDEFGHIJKLMNOPQRSTU VWX" 我的字母数组为24个字符:“ ABCDEFGHIJKLMNOPQRSTU VWX”

I want collect all case with: 3 unique characters. 我想收集所有情况:3个独特的角色。

First case: ABC, DEF, GHI, JKL, MNO, PQR, STU, VWX 第一种情况:ABC,DEF,GHI,JKL,MNO,PQR,STU,VWX

This is a little late coming, but for anyone else reading over this: If you are looking to split a string into 3-character chunks, try PHP's built in str_split() function. 这有点迟了,但是对于其他任何人来说,请读以下内容:如果您希望将字符串分成3个字符的块,请尝试使用PHP的内置str_split()函数。 It takes in a $string and $split_length argument. 它接受$string$split_length参数。 For example: 例如:

$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWX';
$grouped  = str_split($alphabet, 3);

var_export( $grouped );

This outputs the following array: 这将输出以下数组:

array ( 0 => 'ABC', 1 => 'DEF', 2 => 'GHI', 
        3 => 'JKL', 4 => 'MNO', 5 => 'PQR', 
        6 => 'STU', 7 => 'VWX', )

This works for the example given in the question. 这适用于问题中给出的示例。 If you want to have every possible combination of those 24 letters, Artefacto's answer makes more sense. 如果您想让这24个字母尽可能组合,Artefacto的答案就更有意义了。

$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$c = strlen($alphabet);
$result = array();

for ($i = 0; $i < $c; ++$i) {
    $current0 = $i;
    for ($j = 0; $j < $c; ++$j) {
        if ($current0 == $j) continue;
        $current1 = $j;
        for ($k = 0; $k < $c; ++$k) {
            if (isset($current0 == $k || $current1 == $k)) continue;
            $result[] = $alphabet[$i].$alphabet[$j].$alphabet[$k];
        }
    }
}

Hope I understood your question right. 希望我理解你的问题对。 This one iterates over the alphabet in three loops and always skips the characters which are already used. 该代码在三个循环中迭代字母,并始终跳过已使用的字符。 Then I push the result to $result. 然后,我将结果推送到$ result。

But better try the script with only five letters ;) Using alls strlen($alphabet) (don't wanna count now...) will need incredibly much memory. 但是最好只用五个字母试试这个脚本;)使用alls strlen($ alphabet)(现在不要计数...)将需要非常多的内存。

(I am sure there is some hacky version which is faster than that, but this is most straightforward I think.) (我敢肯定有一些hacky版本比这更快,但是我认为这是最简单的。)

There's a 1:1 relationship between the permutations of the letters of the alphabet and your sets lists. 字母的排列与集列表之间存在1:1的关系。 Basically, once you have a permutation of the alphabet, you just have to call array_chunk to get the sets. 基本上,一旦您有了字母的排列,您只需要调用array_chunk即可获取集合。

Now, 24! 现在24岁! of anything (that is 620448401733239439360000) will never fit in memory (be it RAM or disk), so the best you can do is to generate a number n between 1 and 24! 任何东西(即620448401733239439360000)将永远无法容纳在内存中(无论是RAM还是磁盘),因此,最好的办法是生成124!之间的数字n 24! (the permutation number) and then generate such permutation. (排列编号),然后生成此类排列。 For this last step, see for example Generation of permutations following Lehmer and Howell and the papers there cited. 对于最后一步,请参阅例如Lehmer和Howell之后的置换生成以及其中引用的论文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM