Skip to content
Merged
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
17 changes: 17 additions & 0 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,23 @@ public static function set(&$array, $key, $value)
return $array;
}

/**
* Push an item into an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string|int|null $key
* @param mixed $values
* @return array
*/
public static function push(ArrayAccess|array &$array, string|int|null $key, mixed ...$values): array
{
$target = static::array($array, $key, []);

array_push($target, ...$values);

return static::set($array, $key, $target);
}

/**
* Shuffle the given array and return the result.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ public function testAdd()
$this->assertEquals(['category' => ['type' => 'Table']], Arr::add(['category' => ['type' => 'Table']], 'category.type', 'Chair'));
}

public function testPush()
{
$array = [];

Arr::push($array, 'office.furniture', 'Desk');
$this->assertEquals(['Desk'], $array['office']['furniture']);

Arr::push($array, 'office.furniture', 'Chair', 'Lamp');
$this->assertEquals(['Desk', 'Chair', 'Lamp'], $array['office']['furniture']);

$array = [];

Arr::push($array, null, 'Chris', 'Nuno');
$this->assertEquals(['Chris', 'Nuno'], $array);

Arr::push($array, null, 'Taylor');
$this->assertEquals(['Chris', 'Nuno', 'Taylor'], $array);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Array value for key [foo.bar] must be an array, boolean found.');

$array = ['foo' => ['bar' => false]];
Arr::push($array, 'foo.bar', 'baz');
}

public function testCollapse()
{
// Normal case: a two-dimensional array with different elements
Expand Down