How do you push multiple items to array PHP

Summary:

There are 2 ways to add multiple elements to a PHP array. You can use the spread method or use the array_merge function. Check out the article for examples.

Push multiple items to array PHP

PHP arrays are dynamic, thus extendable at run time. It means that you can push multiple items to array PHP, and it won’t bother you with exceptions like a static array would in Java or C++. PHP array_push function is super helpful in pushing into an array though iteration also works.

add element to an array

This article includes examples of pushing multiple items to an array. The first example uses a loop, followed by other examples of using array_push and array_merge. So, let’s just get to the examples now.

Push multiple items to array PHP with loop

We have an array of numbers from six to ten, and we need to push these to an array that already has numbers from one to five. The following example adds items iteratively to an array in PHP.

<?php
 
$numbers = [1, 2, 3, 4, 5];
 
$some_more_numbers = [6, 7, 8, 9, 10];
 
foreach($some_more_numbers as $nums)
{
    $numbers[] = $nums; //Push each number iteratively.
}
 
/*
OUTPUT
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*/
 
?>

So, $numbers[] syntax tells PHP to push $nums into it. The loop pushes numbers from six to ten iteratively. In this way, we push multiple items to array PHP.

Quick Note

PHP array_push pushes one or more elements to an array. Use array_push when there multiple elements to push. Use the example syntax for a single element push to avoid the overhead of a function call.

Push multiple items to array PHP with array_push

PHP array_push pushes one or more items to an array. Following is the function signature of array_push.

array_push(array &$array, mixed ...$values): int

So, the function takes an input array as an argument followed by items that we need to push into the input array. A comma separates these items, as the following example shows.

<?php
$numbers = [1, 2, 3, 4, 5];
 
$some_more_numbers = [6, 7, 8, 9, 10];
 
foreach($some_more_numbers as $nums)
{
    array_push($numbers, $nums);
}
 
/*
OUTPUT
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*/
 
?>

You can see that nothing has changed so far. The example looks pretty similar to the previous one except that we’re using array_push now. Pushing the whole array means adding an array within an array. Luckily, there’s a solution –  the “Spread Operator.”

Push multiple items to array PHP with spread operator

PHP 7.4 introduced the “Spread Operator.” PHP uses three dots syntax for spread operation. Just prefix an array with three dots, and PHP spreads the elements in place. This operation applies to the examples we have seen already.

<?php
 
$numbers = [1, 2, 3, 4, 5];
 
$some_more_numbers = [6, 7, 8, 9, 10];
 
array_push($numbers, ...$some_more_numbers);
 
/*
OUTPUT
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*/
 
?>

Voila! The example reduces to one function call. That’s the power of the spread operator. It lets you push multiple items to array PHP without using any loop explicitly.

Push multiple items to array PHP using array_merge

PHP array_merge is another way to push multiple items to array PHP. The function has some caveats that you can read by following the link. Here we’ll see it through an example.

<?php
 
$numbers = [1, 2, 3, 4, 5];
 
$some_more_numbers = [6, 7, 8, 9, 10];
 
$merged_numbers = array_merge($numbers, $some_more_numbers);
 
/*
OUTPUT
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*/
 
?>

Perfect! All done in one call. Also, the spread operator tends to be faster than the array_merge, and this is obvious because the spread operator is a PHP construct rather than a function. They work differently under the hood.

Push multiple items to an associative array in PHP

The associative array has keys and values. Unfortunately, the spread operator doesn’t work with string keys and raises an exception. So, we are left with either the good old loop way or the array_merge.

The array_merge is just a function call, demonstrating the loop example here.

<?php
 
$scores = ['Adam' => 10, 'Shaun' => 11, 'Robert' => 15];
 
$some_more_scores = ['Ron' => 18, 'Kylie' => 22];
 
foreach($some_more_scores as $k => $v)
{
    $scores[$k] = $v;
}
 
/*
OUTPUT
Array
(
    [Adam] => 10
    [Shaun] => 11
    [Robert] => 15
    [Ron] => 18
    [Kylie] => 22
)
*/
?>

Voila! The example creates a key and assigns it a value iteratively in a loop.

Conclusion

This article includes examples of pushing multiple items to array PHP using a loop or the spread operator. The spread operator syntax is more compact and cleaner than using a loop. The foreach loop is faster than the spread operator, but that doesn’t matter much in trivial cases. So, what option would you pick?

That’s all for the article. We hope you liked it. To see more articles and tutorials about PHP, stay tuned to FuelingPHP.

Want to learn more about PHP?


Article Categories

Article Tags

© 2022 Confident.Systems