Php to find a combination of all A (4pr 2)?

for example, if you want to get AB, AC, BA, BC, CA, and CB.
but the number of ABC varies, ask the algorithm expert, how to write a function to get the array of all cases?

Php
Mar.03,2021

No code is only available for other An2 models to write a general

.
function An2(array $arr)
{
    $out = [];
    for ($i = 0; $i < count($arr) - 1; $i PP) {
        for ($j = $i + 1; $j < count($arr); $jPP) {
            $out[] = [$arr[$i], $arr[$j]];
            $out[] = [$arr[$j], $arr[$i]];
        }
    }
    return $out;
}

print_r(An2([1,2]));
Menu