What is the difference between function and assignment for array?

13

I have recently made codes in my projects where I need to add items to an array, so I do not know if I should use native language functions like this:

array_push($meu_array, 50);

or simply assign the value, for example:

$meu_array[] = 50;

I wanted to know the difference between them? if there is one that is more performative or more advisable to do, it would be interesting also to know if there is any parameter that I should take into account when a similar situation happens

    
asked by anonymous 05.01.2018 / 17:30

3 answers

17

Both are not equivalent, array_push adds an item, and $meu_array[0] = 50; would edit an existing item or create an index at zero.

The equivalent of array_push is [] without the index, thus:

$meu_array[] = 50;

However array_push has a difference

What sets array_push from [] is that with it you can add multiple items, like this:

array_push($meu_array, 50, 60, 100, 9, 'a');

Whether it would be equivalent to:

$meu_array[] = 50;
$meu_array[] = 60;
$meu_array[] = 100;
$meu_array[] = 9;
$meu_array[] = 'a';

So in this case it might be more interesting to use array_push

Performance between [] and array_push

Supposedly, the use of [] is a bit better (it's relative), but it's probably micro-optimization, that is, the difference is insignificant, but to write I think it's simpler to use [] than array_push and besides simple you write less:

$meu_array[] = 50;
array_push($meu_array, 50);

To compare performance you can use the phplegends / tests library, requires composer the ideal test is to make it into a large array, so just to test $minha_array = range(0, 10000); this should suffice.

would look like this:

<?php
use PHPLegends\Tests\Bench;
use PHPLegends\Tests\BenchObject;

require 'vendor/autoload.php'; //Composer autoload

$minha_array1 = range(0, 10000);
$minha_array2 = range(0, 10000);

$bench = new Bench;
$bench->defaultCicles(9999); //9999 execuções

$test1 = $bench->addTest(function () use (&$minha_array1) {
    $minha_array1[] = 50;
});

$test2 = $bench->addTest(function () use (&$minha_array2) {
    array_push($minha_array2, 50);
});

$bench->run();

echo 'Test #1 (time): ', $test1->time(), PHP_EOL;
echo 'Test #2 (time): ', $test2->time(), PHP_EOL;

Result in PHP5.4:

Test #1 (time): 0.041198015213013
Test #2 (time): 0.051141023635864

Result in PHP7.2:

Test #1 (time): 0.0042929649353027
Test #2 (time): 0.0053369998931885

The difference in PHP7.2 seems insignificant, in other versions of PHP the result may be quite different, as in PHP5.4, or even vary between operating systems such as Windows and Linux

In any case, this is micro-optimization and generally we do not have to worry, except if you actually do thousands of executions, but for all other cases use whatever you like best. >

Now multiple additions:

...
$test1 = $bench->addTest(function () use (&$minha_array1) {
    $minha_array1[] = 50;
    $minha_array1[] = 60;
    $minha_array1[] = 100;
    $minha_array1[] = 9;
    $minha_array1[] = 'a';
});

$test2 = $bench->addTest(function () use (&$minha_array2) {
    array_push($minha_array2, 50, 60, 100, 9, 'a');
});
...

Result in PHP5.4:

Test #1 (time): 0.053055047988892
Test #2 (time): 0.056273937225342

Result in PHP7.2:

Test #1 (time): 0.0079491138458252
Test #2 (time): 0.0065710544586182

Note that multiple inserts were slightly better using array_push , however the difference is minimal, in all tests, for 9999 repetitions is that it seems to be better, but probably the result varies a lot, ie there is no reason to worry about one another.

    
05.01.2018 / 17:41
6

In these examples you can not compare well. One adds an element to the array , or another assigns value to element 0 of array . They only do the same thing if the array is empty, so the semantics are different and you only get the same result by coincidence.

Obviously the first one will always create a new element in array increasing its size. The second one will only create the element if index 0 does not exist.

The first will always add an element with the numerical index incremented with the largest number found. The second can use the key you want and does not even need to be numeric.

According to a answer in OS the second is faster. I do not know if it still applies to the current versions. Anyway I find it cleaner to do this way. And what the documentation makes sense.

There is a test in the documentation that shows a brutal difference . Remembering that this may not be valid for the current versions. But there's another post showing a much lesser difference . And there's another one showing that if you're going to do multiple inserts array_push() may be faster .

To get the same semantics the assignment would have to look like this:

$meu_array[] = 50;
    
05.01.2018 / 17:38
4

Items added with array_push() have only their numeric indexes only. In assignment form it can be both numerical and associative.

$arr = array('e1' => 1, 'e2' => 2);
array_push($arr, 3);

Output:

Array
(
    [e1] => 1
    [e2] => 2
    [0] => 3
)

With attribution:

$arr = array('e1' => 1, 'e2' => 2);
$arr['e3'] = 3;

Output:

Array
(
    [e1] => 1
    [e2] => 2
    [e3] => 3
)
    
05.01.2018 / 17:37