Array_merge vs array_value to reset array index

I have 1 array that I want to reindex. I found that array_values and array_merge functions can do the job (and I don't need 2 arrays for the array_merge function to work).

What is faster for a very large array? I would appreciate it, but I do not know how and do not have a large array.

Before re-indexing:

 Array ( [0] => AB [4] => EA [6] => FA [9] => DA [10] => AF ) 

After re-indexing:

 Array ( [0] => AB [1] => EA [2] => FA [3] => DA [4] => AF ) 
+7
arrays php indexing array-merge
source share
4 answers

I have not done any tests either - and if you need to be sure, you have to do it.

However, I would suspect that if one of them is preferable to the other, array_values ​​() would be a suitable way.

In the end, what you want to do is exactly what the_values ​​() array was created for.

+2
source share

I have a checkmark, the value of array_value is 3 times faster (sorry for answering my own question, the comments section does not save the format)

for and array with elements 8043

array values ​​took 0.003291130065918 seconds.

array merge took 0.0096800327301025 seconds.

$ shuf is an unindexed array

Below is the code to run the test (copied from the site)

  $sha1_start = microtime(true); $arraymerge = array_merge ($shuf); $shal_elapsed = microtime(true) - $sha1_start; $start = microtime(true); $arrayvalue = array_values ($shuf); $elapsed = microtime(true) - $start; echo "<br>array values took $elapsed seconds."; echo "<br>array merge took $shal_elapsed seconds."; 
+3
source share

array_values ​​is designed to do exactly what you want. array_merge is designed to do something else, and you have a workaround to get it working on your case. (although you may have problems if a non-numeric value is forgotten in the indexes).

I don’t know if there are significant performance differences, but certainly the code written with array_values ​​is easier to read. And I don’t think that a function designed for something is slower than one function designed to do something else.

Hope this helps.

+2
source share

It is important to note that array_merge() will only reset the keys of the array if there are no string keys in the array.

+1
source share

All Articles