Home > Backend Development > PHP Tutorial > Find different combinations of n elements in a one-dimensional array and return the array

Find different combinations of n elements in a one-dimensional array and return the array

WBOY
Release: 2016-07-06 13:52:57
Original
1414 people have browsed it

array('a1','b2','c3','d4','e5','x1','y1','z1');
For example, the above array

I want different combinations of the three elements in this array element, for example:
array('a1','b2','c3');
array('a1','b2', 'd4');
array('a1','b2','e5');
array('a1','b2','x1');
array('a1', 'b2','y1');
....

And finally return such an array

array(

<code>array('a1','b2','c3'),
array('a1','b2','d4'),
array('a1','b2','e5'),
array('a1','b2','x1'),
array('a1','b2','y1'),
......</code>
Copy after login
Copy after login

)

Reply content:

array('a1','b2','c3','d4','e5','x1','y1','z1');
For example, the above array

I want different combinations of the three elements in this array element, for example:
array('a1','b2','c3');
array('a1','b2', 'd4');
array('a1','b2','e5');
array('a1','b2','x1');
array('a1', 'b2','y1');
....

And finally return such an array

array(

<code>array('a1','b2','c3'),
array('a1','b2','d4'),
array('a1','b2','e5'),
array('a1','b2','x1'),
array('a1','b2','y1'),
......</code>
Copy after login
Copy after login

)

Three-layer foreach solution

<code>$data = array('a1','b2','c3','d4','e5','x1','y1','z1');

foreach ($data as $k_1 => $v_1) {
    foreach ($data as $k_2 => $v_2) {
        foreach ($data as $k_3 => $v_3) {
            if ($v_1 !== $v_2 && $v_1 !== $v_3 && $v_2 !== $v_3) {
                var_dump([$v_1,$v_2,$v_3]);
            }
        }    
    }
}</code>
Copy after login

As for permutations and combinations, the indexes of three numbers have sizes after all. Arrange them from small to large.
Set them as a b c d, and then (this is a math problem)

Thanks for the invitation. If you haven’t learned math well, go check out the data sorting/merging function usage in the manual

<code><?php
$arr = array('a','b','c','d');
$result = array();
$t = getCombinationToString($arr, 3);
print_r($t);

function getCombinationToString($arr, $m) {
    if ($m ==1) {
        return $arr;
    }
    $result = array();

    $tmpArr = $arr;
    unset($tmpArr[0]);
    for($i=0;$i<count($arr);$i++) {
        $s = $arr[$i];
        $ret = getCombinationToString(array_values($tmpArr), ($m-1));

        foreach($ret as $row) {
            $result[] = $s . $row;
        }
    }

    return $result;
}
?>
</code>
Copy after login

Array
(

<code>[0] => abc
[1] => abd
[2] => acc
[3] => acd
[4] => adc
[5] => add
[6] => bbc
[7] => bbd
[8] => bcc
[9] => bcd
[10] => bdc
[11] => bdd
[12] => cbc
[13] => cbd
[14] => ccc
[15] => ccd
[16] => cdc
[17] => cdd
[18] => dbc
[19] => dbd
[20] => dcc
[21] => dcd
[22] => ddc
[23] => ddd</code>
Copy after login

)
Give you an idea

Are array('a1','b1','c1') and array('c1','a1','b1') considered duplicates?

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template