2

On my everyday office work, I have to deal with different types of multidimensional array and sometime I flat them to single dimension for my work purpose. Most of the time I prefer call_user_func_array with with array_merge param. Actually I want to know how they internally work specially the ... operator way and which method is faster?

<?php
$data = [[1, 2], [3], [4, 5]];
print '<pre>';
print_r(array_merge(... $data)); // [1, 2, 3, 4, 5];
print_r(call_user_func_array('array_merge',$data)); // [1, 2, 3, 4, 5];
print_r(array_reduce($data, 'array_merge', []));   // [1, 2, 3, 4, 5];
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103

1 Answers1

1

Actually I want to know how they internally work

The documentation for variable-length argument lists may give you some info. I won't link the documentation for the other functions you've mentioned as they are easy to google.

I don't know how they work internally, but you could dig through the PHP source code to find out. Bear in mind that it is written in C.

which method is faster?

Check out this answer on measuring performance of PHP code. TLDR: Measure microtime, or use xdebug's profiling tool.

Jim Wright
  • 5,905
  • 1
  • 15
  • 34