PHP notes (6)-Array add element
Today I learned a new way to add an element to the PHP array.
Previously always added with the push () function:
$arr = Array (); Array_push ($arr, El1, El2 ... ELN);
?
But in fact there is a more direct and convenient approach:
$arr = Array (); $arr [] = El1; $arr [] = El2; ... $arr [] = ELN;
?
And experiments have proved that the second method is more efficient than the first method is nearly one times higher!
Let's take a look at the following example:
$t = Microtime (true); $array = Array (); for ($i = 0; $i < 10000; $i + +) { $array [] = $i;} print Microtime (true)-$t; print '
'; $t = Microtime (true); $array = Array (); for ($i = 0; $i < 10000; $i + +) { Array_push ($array, $i);} print Microtime (true)-$t;
Run the script with the result:
? wrote
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[]
?
It's really a long time.