No, there is no faster way than your implemented code. All other methods will be slower due to the overhead of a function call. For a small array the difference will be trivial, but for a large one (100 members or so, depending on implementation), the difference can be huge...
You could array_map
it, but I'd stick with the raw PHP you posted above... It's easier to maintain and IMHO more readable...
After all, tell me which at a glance tells you what 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 does it for me the best. It's immediately obvious without knowing anything what it's doing. The others require knowledge of array_map
semantics to understand. Couple that with the fact that array_map
is slower, and it's a double win for foreach
.
Code should only be as elegant as necessary. It should be readable above all else...