๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.array-push.php
PHP: array_push - Manual
array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as: <?php $array[] = $var; ?> repeated for each passed value.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_array_push.asp
PHP array_push() Function
Loops While Loop Do While Loop For Loop Foreach Loop Break Statement Continue Statement PHP Functions PHP Arrays
Discussions

Which is faster in PHP, $array[] = $value or array_push($array, $value)? - Stack Overflow
What's better to use in PHP for appending an array member, $array[] = $value; or array_push($array, $value); ? Though the manual says you're better off to avoid a function call, I've also read $... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why do functions like array_push or usort directly modify an array but functions like array_slice needs to return the modified array?
There is a technical reasoning behind it. First array_push(), array_pop(), array_shift(), array_unshift(), and the various sorting methods are suppose to be used to change the parent data construct, the array. This is so you can use arrays in PHP to create a Stack or Queue, or Sort the array effectively in memory. While the methods such as array_slice, is intended to take the parent data construct, the array, and return a subarray of that array for usage in whatever function. This is usually done when you want to do something with the subarray but you still want to keep the parent array intact. via some sorting or searching algorithm, for example. TL;DR There are Technical reasons for why some functions in PHP alter the parent data construct and others return a new data construct. More on reddit.com
๐ŸŒ r/PHP
31
46
August 29, 2015
array_push() is being ignored completely.

You're calling unpickArray recursively, but not returning the results to the calling parent. Variable scope is tied to the called function, so $keyArray and $valArray aren't getting filled with each nested iteration - just locally. You need something like:

list($keyArray, $valArray) = unpickArray($b);

But overall, this is a really ugly bit of code. array_push() is considered less readable than the syntactical sugar of [].

$k = key($a);

Try getting rid of that nastiness and use this as your foreach instead:

foreach($a as $k=>$b){

Also, try to avoid renaming all of your variables before using them, it makes things way confusing.

More on reddit.com
๐ŸŒ r/PHP
12
14
August 11, 2009
Assigning array with [] vs without?
Assigning to [] will put a new entry at the end of that array. Think of it like using array.push() in js. More on reddit.com
๐ŸŒ r/PHPhelp
3
2
August 29, 2017
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ php-array-push
PHP array_push โ€“ Add Elements to an Array - PHPpot
Tutorial to learn adding elements to an array in PHP with its native function array_push().
๐ŸŒ
Audio Science Review
audiosciencereview.com โ€บ forums
Audio Science Review (ASR) Forum
2 weeks ago - Audio reviews, science and engineering discussions.
๐ŸŒ
Oreate AI
oreateai.com โ€บ blog โ€บ mastering-php-the-art-of-pushing-values-to-arrays โ€บ c482defb8fbe7b148121a8992e4f72c1
Mastering PHP: The Art of Pushing Values to Arrays - Oreate AI Blog
January 8, 2026 - Explore how PHP's `array_push()` function works for adding elements to arrays and discover best practices for efficient coding.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-insert-an-item-at-the-beginning-of-an-array-in-php
How to insert an item at the beginning of an array in PHP ? - GeeksforGeeks
August 21, 2024 - Use array_push() to add the new element to the end of the reversed array. Reverse the array again to get the final array with the new element at the beginning. Example: PHP ยท
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ php array_push() function
PHP array_push() Function - Scaler Topics
September 22, 2023 - The array_push() function in PHP is used to append one or more elements to the end of an array. It modifies the original array and returns the new number of elements in the array.
Find elsewhere
๐ŸŒ
Laravel
laravel.com โ€บ docs โ€บ 12.x โ€บ collections
Collections | Laravel 12.x - The clean stack for Artisans and agents
The pad method will fill the array with the given value until the array reaches the specified size. This method behaves like the array_pad PHP function.
๐ŸŒ
Reddit
reddit.com โ€บ r โ€บ unRAID
Unraid: Unleash Your Hardware
July 15, 2011 - As I wrote in the title, the disk that is disabled looks fine (SMART is passed) andche cables are checked that work. Now I'm trying to turn of the array to unmount and remount the drive to rebuild it or make it active again but the array shoutdown is stuck.
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ mongodb-user โ€บ c โ€บ 6WnunZBc8MQ โ€บ m โ€บ fhHd0NxFXmcJ
PHP: Push value into "array" only when it doesn't exist.
In PHP, that's: $c->update(array("_id" => "fruits", "types" => array('$ne' => "orange")), array('$push' => array("types" => "orange"))); Maybe you were missing the single quotes are '$ne' or '$push'?
Top answer
1 of 9
180

I personally feel like $array[] is cleaner to look at, and honestly splitting hairs over milliseconds is pretty irrelevant unless you plan on appending hundreds of thousands of strings to your array.

I ran this code:

$t = microtime(true);
$array = array();
for($i = 0; $i < 10000; $i++) {
    $array[] = $i;
}
print microtime(true) - $t;
print '<br>';
$t = microtime(true);
$array = array();
for($i = 0; $i < 10000; $i++) {
    array_push($array, $i);
}
print microtime(true) - $t;

The first method using $array[] is almost 50% faster than the second one.

Some benchmark results:

Run 1
0.0054171085357666 // array_push
0.0028800964355469 // array[]

Run 2
0.0054559707641602 // array_push
0.002892017364502 // array[]

Run 3
0.0055501461029053 // array_push
0.0028610229492188 // array[]

This shouldn't be surprising, as the PHP manual notes this:

If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

The way it is phrased I wouldn't be surprised if array_push is more efficient when adding multiple values. Out of curiosity, I did some further testing, and even for a large amount of additions, individual $array[] calls are faster than one big array_push. Interesting.

2 of 9
45

The main use of array_push() is that you can push multiple values onto the end of the array.

It says in the documentation:

If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

๐ŸŒ
Parthpatel
parthpatel.net โ€บ home โ€บ php โ€บ php: array_push function | php add to array tutorial
PHP: array_push Function | PHP Add to Array Tutorial | Parth Patel - a Web Developer
October 26, 2020 - The array_push() is used to insert new elements at the end of given array. It can accept any numbers of elements to be pushed at the end of the array.
๐ŸŒ
TecAdmin
tecadmin.net โ€บ append-item-to-array-in-php
How to Append an Item to Array in PHP โ€“ TecAdmin
April 26, 2025 - This tutorial uses array_push() function to insert or append a new element to end of the Array.
๐ŸŒ
w3resource
w3resource.com โ€บ php โ€บ function-reference โ€บ array_push.php
PHP : array_push() function - w3resource
August 19, 2022 - The array_push() function is used to add one or more elements onto the end of an array. The length of array increases by the number of variables pushed.
๐ŸŒ
Expo Documentation
docs.expo.dev โ€บ expo sdk โ€บ notifications
Notifications - Expo Documentation
A library that provides an API to fetch push notification tokens and to present, schedule, receive and respond to notifications.
๐ŸŒ
W3Schools
w3schools.sinsixx.com โ€บ php โ€บ func_array_push.asp.htm
PHP array_push() Function
Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building.
๐ŸŒ
Wrox
p2p.wrox.com โ€บ beginning-php โ€บ 56921-php-array_push-associative-arrays.html
PHP array_push() for associative arrays
Hello There, I am hoping that someone can help me. I am struggling a little bit with PHP arrays. I am relatively new to PHP programming. I am in the
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ php โ€บ axuo1zyi โ€บ php-array-push-example
How to push an element into a PHP array?
To add elements to a PHP array, you can use the array_push($array, $val1, $val2, ....) function. The array_push() function adds one or more elements to the end of the array and automatically increases the array's length.