Q:

PHP - Looping once through each array combinaton

The problems looks a little bit trivial, but I did not find an answer:
I have a one dimensional array, and I'm looking to loop through each combination between itself, only one time per combination.
I'm looking for something like: 
public function foo($array)
{
    $array = [1,2,3];
    $arrayCombinations = $this->arrayPermutations($array);

    foreach($arrayCombinations as $combination) {
        echo $combination;
    } 
}

public function arrayPermutations($elements)
{
    $arrayPermutations = array();

    foreach ($elements as $element1) {
        foreach ($elements as $element2) {
            if ($element1 != $element2) {
                $permutationA = array($element1, $element2);
                $permutationB = array($element2, $element1);
                if (!in_array($permutationA, $arrayPermutations) && !in_array($permutationB, $arrayPermutations)) {
                    $arrayPermutations[] = $permutationA;
                }
            }
        }
    }

    return $arrayPermutations;
}

That would give:
1-2
1-3
2-3

I'm looking for combination between exactly 2 elements, and excluding the combination with the element itself.
Since I'm going to reuse this behavior many times, I would like to find a clean way to do it.
Would I be better off just coding the behavior manually?

A:

Something like this?
function fooFunction($array) {
    $res = array();
    foreach ($array as $i => $valI) {
        foreach ($array as $j => $valJ) {
            if ($i < $j)
                $res[] = $valI."-".$valJ;
        }
    }
    return $res;
}

Or maybe more directly:
function fooFunction($array) {
    $res = array();

    $n = sizeof($array);
    for ($i = 0; $i < $n; $i++)
        for ($j = $i + 1; $j < $n; $j++)
            $res[] = $array[$i]."-".$array[$j];

    return $res;
}

The most general form would probably be to take a callback function, which can then do whatever you want:
function fooFunction($array, $callback) {
    $n = sizeof($array);
    for ($i = 0; $i < $n; $i++)
        for ($j = $i + 1; $j < $n; $j++)
            $callback($array[$i], $array[$j]);
}

fooFunction([1, 2, 3], function($a, $b) {
    echo $a."-".$b."\n";
});

