This question already has answers here:
Which is faster in PHP, $array[] = $value or array_push($array, $value)?

(9个答案)


5年前关闭。




我需要将从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']);
}

最佳答案

您可以运行它,并在某些情况下看到array_push较慢:

http://snipplr.com/view/759/speed-test-arraypush-vs-array/

运行您的代码。享受。

10-07 14:43