Reputation: 22197
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.
Upvotes: 122
Views: 173388
Reputation: 129
They are the same, but array_push
makes a loop in its parameter which is an array and performs $array[]=$element
.
Upvotes: -1
Reputation: 169
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;
...
}
Upvotes: -1
Reputation: 45
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);
// 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.
Upvotes: 1
Reputation: 2922
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.
Upvotes: -1
Reputation: 921
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.
Upvotes: 45
Reputation: 311
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.
Upvotes: 18
Reputation: 21
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
Upvotes: 2
Reputation: 1635
array_push — Push one or more elements onto the end of array
Take note of the words "one or more elements onto the end"
to do that using $arr[]
you would have to get the max size of the array
Upvotes: 4
Reputation: 53238
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[]
.
Upvotes: 157
Reputation: 2759
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.
Upvotes: 7