以特殊顺序同时迭代多个迭代器

Lets say I have two iterators:

$a = new ArrayIterator(array(4, 3, 2));
$b = new ArrayIterator(array(6, 5, 1));

Now I want to iterate them in a way to output the following:

 a b sum

 4 6  10 
 4 5  9 
 3 6  9 
 3 5  8 
 2 6  8 
 2 5  7 
 4 1  5 
 3 1  4 
 2 1  3 

So I want to iterate through all possible combinations. Additionally I want it in a way that the ones with the summed highest value will come first.

I don't want to use arrays (so also no array sorting), I need to do it with iterators (because of constraints from my original case).

It's your required ordering that throws things. You can't do it without some form of sort; but a heap datastructure may be an option:

$a = new ArrayIterator(array(4, 2, 3));
$b = new ArrayIterator(array(1, 6, 5));

class MyHeap extends SplHeap {
    public function compare($v1, $v2) {
        if ($v1->sum === $v2->sum) return 0;
        return $v1->sum < $v2->sum ? -1 : 1;
    }
}

$heap = new MyHeap();
foreach($a as $val1) {
    foreach($b as $val2) {
        $heap->insert(
            (object) [
                'val1' => $val1,
                'val2' => $val2,
                'sum' => $val1+$val2
            ]
        );
    }
}

$heap->top();
foreach($heap as $value) {
  echo $value->val1, ' ', $value->val2, ' ', $value->sum, PHP_EOL;
}