Difference between array_push() and $array[] =

2019-01-10 05:32发布

问题:

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.

回答1:

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[].



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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



回答6:

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



回答7:

both are the same, but array_push makes a loop in it's parameter which is an array and perform $array[]=$element



回答8:

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.



回答9:

In normal words..

// produces: array(0=>'foo', 1=>'bar');
$arr[] = 'foo'
$arr[] = 'bar'

//produces: array('foo', 'bar');
array_push($arr, 'foo', 'bar');


回答10:

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



标签: php arrays push