array_push() 与 $array[] = .... 哪个最快?
我需要将从 MySQL 接收到的值添加到数组(PHP)中。 这就是我所得到的:
$players = array();
while ($homePlayerRow = mysql_fetch_array($homePlayerResult)) {
$players[] = $homePlayerRow['player_id'];
}
这是唯一的方法吗?
另外,以下是否更快/更好?
$players = array();
while ($homePlayerRow = mysql_fetch_array($homePlayerResult)) {
array_push($players, $homePlayerRow['player_id']);
}
I need to add values received from MySQL into an array (PHP). Here is what I've got:
$players = array();
while ($homePlayerRow = mysql_fetch_array($homePlayerResult)) {
$players[] = $homePlayerRow['player_id'];
}
Is this the only way of doing it?
Also, is the following faster/better?
$players = array();
while ($homePlayerRow = mysql_fetch_array($homePlayerResult)) {
array_push($players, $homePlayerRow['player_id']);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于...
因此,归结为您有多少数据想要在任何特定时刻塞入该数组。
此外,还有一个后退。 如果使用 array_push 调用数组时引用的数组不存在,则会出现错误。 如果您使用 $array[],将为您创建数组。
It depends...
So it boils down to how much data you want to cram into that array at any particular moment.
Additionally, there's a fall-back. If the array-referenced doesn't exist when you call it using array_push, you'll bump an error. If you use $array[], the array will be created for you.
您可以运行它并看到 array_push 在某些情况下速度较慢:
http ://snipplr.com/view/759/speed-test-arraypush-vs-array/
运行您的代码。 享受。
You can run it and see that array_push is slower in some cases:
http://snipplr.com/view/759/speed-test-arraypush-vs-array/
Run your code. Enjoy.