Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use ArgumentCountError;
use ArrayAccess;
use ArrayIterator;
use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use JsonSerializable;
use MultipleIterator;
use Random\Randomizer;
use SortDirection;
use Traversable;
Expand Down Expand Up @@ -1308,6 +1310,20 @@ public static function whereNotNull($array)
return static::where($array, fn ($value) => ! is_null($value));
}

public static function concurrent(array ...$arguments)
{
$typeFlag = array_all($arguments, fn ($value) => array_is_list($value))
? MultipleIterator::MIT_KEYS_ASSOC
: MultipleIterator::MIT_KEYS_NUMERIC;
$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY | $typeFlag);

foreach ($arguments as $idx => $array) {
$multipleIterator->attachIterator(new ArrayIterator($array), $idx);
}

return $multipleIterator;
}

/**
* If the given value is not an array and not null, wrap it in one.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\MultipleItemsFoundException;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use stdClass;
use WeakMap;
Expand Down Expand Up @@ -1962,4 +1963,17 @@ public function testPartition()

$this->assertEquals([[0 => 'John', 1 => 'Jane'], [2 => 'Greg']], $result);
}

#[TestWith([[[1, 2], [3, 4]], [2, 6, 4, 8]])]
#[TestWith([[['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4]], [2, 6, 4, 8]])]
public function testConcurrent(array $input, array $output)
{
$result = [];

foreach (Arr::concurrent(...$input) as $key => [$c, $d]) {
array_push($result, $c * 2, $d * 2);
}

$this->assertEquals($output, $result);
}
}
Loading