php array_push() 和 $array[] 的区别 =
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14232766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between array_push() and $array[] =
提问by l2aelba
In the PHP manual, (array_push) says..
在 PHP 手册中,( array_push) 说..
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.
如果您使用array_push()向数组添加一个元素,最好使用$array[] =因为这样就没有调用函数的开销。
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.
我不明白为什么会有很大的不同。
回答by BenM
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.
当您在 PHP 中调用函数(例如array_push())时,调用会产生开销,因为 PHP 必须查找函数引用、找到它在内存中的位置并执行它定义的任何代码。
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[].
Using$arr[] = 'some value';不需要函数调用,而是直接将加法实现到数据结构中。因此,当添加大量数据时,使用$arr[].
回答by Sujit Singh
You can add more than 1 element in one shot to array using array_push,
您可以使用 array_push 一次性将 1 个以上的元素添加到数组中,
e.g. array_push($array_name, $element1, $element2,...)
例如 array_push($array_name, $element1, $element2,...)
Where $element1, $element2,... are elements to be added to array.
其中 $element1, $element2,... 是要添加到数组的元素。
But if you want to add only one element at one time, then other method (i.e. using $array_name[]) should be preferred.
但是如果你想一次只添加一个元素,那么应该首选其他方法(即使用 $array_name[])。
回答by Baig
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.
array_push()如果第一个参数不是数组,则会引发警告。这与$var[]创建新数组的行为不同。
回答by Benjamin Paap
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.
$array[]如果可能,您应该始终使用,因为正如框所述,函数调用没有开销。因此它比函数调用快一点。
回答by ianace
array_push— Push one or more elements onto the end of array
array_push— 将一个或多个元素推送到数组的末尾
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
记下“一个或多个元素到最后”这个词,使用$arr[]你必须获得数组的最大大小
回答by Saravana Kumar
explain: 1.the first one declare the variable in array.
解释: 1.第一个声明数组中的变量。
2.the second array_push method is used to push the string in the array variable.
2.第二个array_push方法用于push数组变量中的字符串。
3.finally it will print the result.
3.finally它会打印结果。
4.the second method is directly store the string in the array.
4.第二种方法是直接将字符串存储在数组中。
5.the data is printed in the array values in using print_r method.
5.使用print_r方法将数据打印在数组值中。
this two are same
这两个是一样的
回答by Sara
both are the same, but array_push makes a loop in it's parameter which is an array and perform $array[]=$element
两者都是一样的,但是 array_push 在它的参数中做了一个循环,它是一个数组并执行 $array[]=$element
回答by Marco
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.
没有人说,但是 array_push 只是将一个元素推送到数组的结尾,其中 $array[index] 可以在任何给定的索引处插入一个值。巨大差距。
回答by Antoniu Livadariu
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:
我知道这是一个旧答案,但让其他人知道两者之间的另一个区别可能会有所帮助,如果您必须为每个循环添加超过 2/3 的值到数组中,则使用速度会更快:
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 forconditional
编辑-忘记关闭for条件的括号

