I was able to play around with your requirements, and I think I got a solution.
Here is what I did to profile your code.
I set up the data array with:
$new = array(
array('a' => 1,
'b' => 2),
array('a' => 3,
'b' => 4),
array('a' => 5,
'b' => 6),
);
Calling it like so:
valuelist($new, 'b');
to produce:
Array
(
[0] => 2
[1] => 4
[2] => 6
)
And the average function time was about 2.1894 E-6 seconds.
However, PHP 5.5 introduced us to the array_column
function! And it happens to do exactly what you want! Yipee!
We can do a big swap-out, and come up with this:
function valuelist($array, $array_column) {
return array_column($array, $array_column);
}
Now when we do the 50k calls, we learn that now the average function time is about 2.6226 E-10 seconds.
However, there is a little overhead due to the user function applied over top of the built-in function. By just calling array_column
without the function does reduce the processing time, but by an amount so small it's hard to even say it's an amount! Just for the sake of cleanliness, I'd remove the user function anyways.
Don't tell me that's not an improvement!
However, without this function, I was unable to get a faster execution. I tried array_filter
, array_map
, and array_walk
. Using use
for the closure, as that proved faster than array_map
's third parameter.