Tuesday, September 27, 2016

Best way to add/insert to array in php

<?php
$array = array();
for (
$x 1$x <= 100000$x++)
{
    
$array[] = $x;
}
?>
takes 0.0622200965881 seconds

and

<?php
$array 
= array();
for (
$x 1$x <= 100000$x++)
{
    
array_push($array$x);
}
?>

takes 1.63195490837 seconds

so if your not making use of the return value of array_push() its better to use the $array[] way.

Hope this helps someone.

Best way to add/insert to array in php

<?php
$array = array();
for (
$x 1$x <= 100000$x++)
{
    
$array[] = $x;
}
?>
takes 0.0622200965881 seconds

and

<?php
$array 
= array();
for (
$x 1$x <= 100000$x++)
{
    
array_push($array$x);
}
?>

takes 1.63195490837 seconds

so if your not making use of the return value of array_push() its better to use the $array[] way.

Hope this helps someone.

Total Pageviews