YS
Size: a a a
YS
vv
v
$array = [2, 2, 4, 4, 2, 4, 4, 2, 4, 4, 2];
$sum = 8;
$result = [];
foreach($array as $key => $item) {
foreach($array as $key2 => $item2) {
if($key != $key2) {
$calc = $item + $item2;
if($calc == $sum) {
array_push($result, [$key, $key2]);
}
}
}
}
$items = new ArrayIterator([2,2,4,2,3,3,6,0,1,3,5,2,2,1]);
$sum = 6;
$getItem = static function($iterator) {
$result = $iterator->current();
$iterator->next();
return $result;
};
$result = [];
while($items->valid()) {
$item = $getItem($items);
if ($items->valid()) {
$nextItem = $getItem($items);
$calc = $item + $nextItem;
if ($calc == $sum) {
array_push($result, [$item, $nextItem]);
}
}
}
A
$items = new ArrayIterator([2,2,4,2,3,3,6,0,1,3,5,2,2,1]);
$sum = 6;
$getItem = static function($iterator) {
$result = $iterator->current();
$iterator->next();
return $result;
};
$result = [];
while($items->valid()) {
$item = $getItem($items);
if ($items->valid()) {
$nextItem = $getItem($items);
$calc = $item + $nextItem;
if ($calc == $sum) {
array_push($result, [$item, $nextItem]);
}
}
}
v
A
v
A
D
D
O
D
D
D
D
D
D
D
D
O