在 PHP 中使用 $array[] = $value 或 array_push($array, $value) 哪个更好?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/559844/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:05:11  来源:igfitidea点击:

What's better to use in PHP, $array[] = $value or array_push($array, $value)?

phparraysperformance

提问by alex

What's better to use in PHP for appending an array member,

在 PHP 中使用什么来附加数组成员更好,

$array[] = $value;

or

或者

array_push($array, $value);

?

?

Though the manual says you're better off to avoid a function call, I've also read $array[]is much slower than array_push(). What are some clarifications or benchmarks?

虽然手册说你最好避免函数调用,但我也读过$array[]array_push(). 有哪些澄清或基准?

回答by Paolo Bergantino

No benchmarks, but 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.

没有基准测试,但我个人觉得看起来$array[]更清晰,老实说,在几毫秒内分割头发是无关紧要的,除非您计划将数十万个字符串附加到您的数组中。

Edit: 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.

使用的第一种方法$array[]比第二种方法快近 50%。

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:

这应该不足为奇,因为 PHP 手册指出:

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[] = 因为这样就没有调用函数的开销。

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

如果array_push在添加多个值时更有效,我不会感到惊讶。编辑:出于好奇,做了一些进一步的测试,即使对于大量添加,单个$array[]调用也比一个 big 快array_push。有趣的。

回答by Inspire

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

array_push() 的主要用途是您可以将多个值推送到数组的末尾。

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.

如果您使用 array_push() 向数组添加一个元素,最好使用 $array[] = 因为这样就没有调用函数的开销。

回答by Benedict Cohen

From the php docs for array_push:

来自php 文档array_push

Note: 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[] = ,因为这样就没有调用函数的开销。

回答by typeoneerror

Word on the street is that [] is faster because no overhead for the function call. Plus, no one really likes PHP's array functions...

街上的说法是 [] 更快,因为函数调用没有开销。另外,没有人真正喜欢 PHP 的数组函数......

"Is it...haystack, needle....or is it needle haystack...ah, f*** it...[] = "

“是……大海捞针……还是大海捞针……啊,他妈的……[] = ”

回答by VolkerK

One difference is that you can call array_push() with more than two parameters, i.e. you can push more than one element at a time to an array.

一个区别是您可以使用两个以上的参数调用 array_push(),即您可以一次将多个元素推送到数组。

$myArray = array();
array_push($myArray, 1,2,3,4);
echo join(',', $myArray);

prints 1,2,3,4

印刷 1,2,3,4

回答by Tom Hallam

A simple $myarray[] declaration will be quicker as you are just pushing an item onto the stack of items due to the lack of overhead that a function would bring.

一个简单的 $myarray[] 声明会更快,因为您只是将一个项目推入项目堆栈,因为函数不会带来开销。

回答by Vinod Kirte

Since "array_push" is a function and it called multiple times when it is inside the loop so it will allocate a memory into the stack. But when we are using $array[] = $value then we just assigning value to array.

由于“ array_push”是一个函数,当它在循环内时会被多次调用,因此它将分配内存到堆栈中。但是当我们使用 $array[] = $value 时,我们只是为数组赋值。

回答by Stefan Gehrig

Second one is a function call so generally it should be slower than using core array-access features. But I think even one database query within your script will outweight 1.000.000 calls to array_push().

第二个是函数调用,所以通常它应该比使用核心数组访问功能慢。但我认为即使是脚本中的一个数据库查询也会超过 1.000.000 次调用array_push().

回答by rybo111

Although the question was more about performance, people will come to this question wondering if it's good practise to use array_pushor $arr[].

虽然这个问题更多地是关于性能的,但人们会问这个问题,想知道使用array_push或是否是一个好习惯$arr[]

The function might mean lesser lines for multiple values:

该函数可能意味着多个值的较少行:

// 1 line:
array_push($arr, "Bob", "Steve");
// versus 2 lines:
$arr[] = "Bob";
$arr[] = "Steve";

However, array_push...

然而array_push...

  • cannot receive the array keys
  • breaks the needle/haystacknaming convention
  • is slower, as has been discussed
  • 无法接收数组键
  • 打破针/大海捞针命名约定
  • 更慢,正如已经讨论过的

I'll be sticking with $arr[].

我会坚持的$arr[]

回答by Jean-Luc Barat

I just wan't to add : int array_push(...)return the new number of elements in the array (php doc). which can be useful and more compact than $myArray[] = ...; $total = count($myArray);.

我只是不想添加:int array_push(...)返回数组中的新元素数(php doc)。这可能比$myArray[] = ...; $total = count($myArray);.

Also array_push(...)is meaningful when variable is used as stack.

array_push(...)当变量被用作堆栈是有意义的。