Difference between array_push() and $array[] =
PhpArraysPushPhp Problem Overview
In the PHP manual, (array_push) says..
> 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.
For example :
$arr = array();
array_push($arr, "stackoverflow");
print_r($arr);
vs
$arr[] = "stackoverflow";
print_r($arr);
I don't understand why there is a big difference.
Php Solutions
Solution 1 - Php
When you call a function in PHP (such as array_push()
), there are overheads to the call, as PHP has to look up the function reference, find its position in memory and execute whatever code it defines.
Using $arr[] = 'some value';
does not require a function call, and implements the addition straight into the data structure. Thus, when adding a lot of data it is a lot quicker and resource-efficient to use $arr[]
.
Solution 2 - Php
You can add more than 1 element in one shot to array using array_push,
e.g. array_push($array_name, $element1, $element2,...)
Where $element1, $element2,... are elements to be added to array.
But if you want to add only one element at one time, then other method (i.e. using $array_name[]) should be preferred.
Solution 3 - Php
The difference is in the line below to "because in that way there is no overhead of calling a function."
> array_push()
will raise a warning if the first argument is not
> an array. This differs from the $var[]
behaviour where a new array is
> created.
Solution 4 - Php
You should always use $array[]
if possible because as the box states there is no overhead for the function call. Thus it is a bit faster than the function call.
Solution 5 - Php
[array_push][1] — Push one or more elements onto the end of array
Take note of the words "one or more elements onto the end" [1]: http://php.net/manual/en/function.array-push.php
to do that using $arr[]
you would have to get the max size of the array
Solution 6 - Php
explain: 1.the first one declare the variable in array.
2.the second array_push method is used to push the string in the array variable.
3.finally it will print the result.
4.the second method is directly store the string in the array.
5.the data is printed in the array values in using print_r method.
this two are same
Solution 7 - Php
both are the same, but array_push makes a loop in it's parameter which is an array and perform $array[]=$element
Solution 8 - Php
Thought I'd add to the discussion since I believe there exists a crucial difference between the two when working with indexed arrays that people should be aware of. Say you are dynamically creating a multi-dimensional associative array by looping through some data sets.
$foo = []
foreach ($fooData as $fooKey => $fooValue) {
foreach ($fooValue ?? [] as $barKey => $barValue) {
// Approach 1: results in Error 500
array_push($foo[$fooKey], $barKey); // Error 500: Argument #1 ($array) must be of type array
// NOTE: ($foo[$fooKey] ?? []) argument won't work since only variables can be passed by reference
// Approach 2: fix problem by instantiating array beforehand if it didn't exist
$foo[$fooKey] ??= [];
array_push($foo[$fooKey], $barKey); // Error 500: Argument #1 ($array) must be of type array
// Approach 3: One liner approach
$foo[$fooKey][] = $barKey; // Instantiates array if it doesn't exist
}
}
Without having $foo[$fooKey]
instantiated as an array beforehand, we won't be able to do array_push
without getting the Error 500
. The shorthand $foo[$fooKey][]
does the heavy work for us, checking if the provided element is an array, and if it isn't, it creates it and pushes the item in for us.
Solution 9 - Php
I know this is an old answer but it might be helpful for others to know that another difference between the two is that if you have to add more than 2/3 values per loop to an array it's faster to use:
for($i = 0; $i < 10; $i++){
array_push($arr, $i, $i*2, $i*3, $i*4, ...)
}
instead of:
for($i = 0; $i < 10; $i++){
$arr[] = $i;
$arr[] = $i*2;
$arr[] = $i*3;
$arr[] = $i*4;
...
}
edit- Forgot to close the bracket for the for
conditional
Solution 10 - Php
No one said, but array_push only pushes a element to the END OF THE ARRAY, where $array[index] can insert a value at any given index. Big difference.