No, there is no faster way than your implemented code. All other methods will be slower due to the overhead of calling the function. For a small array, the difference will be trivial, but for a large (100 members or so, depending on implementation) the difference can be huge ...
You could array_map it, but I would stick with the raw PHP that you posted above ... It is easier to maintain and IMHO more readable ...
In the end, tell me which, at first glance, it says that it does more:
$results = array(); foreach ($array as $value) { $results[] = $value['title']; }
vs
$results = array_map(function($element) { return $element['title']; }, $array );
Or:
$callback = function($element) { return $element['title']; } $results = array_map($callback, $array);
Personally, the first one makes it the best for me. This immediately becomes apparent, not knowing anything that he does. Others require knowledge of the semantics of array_map to understand. A couple that with the fact that array_map slower, and this is a double win for foreach .
The code should only be as elegant as necessary. It should be readable, first of all ...
ircmaxell
source share