0

I'm creating a comma delimited string for an SQL query. I would like to know what is better arraymap or a foreach loop. Here are my two examples:

$group_ids = "";
foreach ($group_array as $group_id) {
    $group_ids .= $group_id . ",";
}
$group_ids = rtrim($group_ids, ',');

vs

$group_ids = "";
array_map(function ($group_id) use ($group_ids) {
    $group_ids .= $group_id . ",";
    return;
}, $group_array);
$group_ids = rtrim($group_ids, ',');

Or is there a better way? Or is there literally not much difference?

3
  • Array_map() is slower than foreach() Commented Oct 3, 2017 at 16:06
  • 2
    Using a prepared statement placeholders (rtrim(str_repeat('?,', count($group_array)), ',')) and then executing the query with $group_array as an array of arguments for binding is even better Commented Oct 3, 2017 at 16:15
  • array_map() should not be called if you are not going to use its return value. If you want a functional iterator, but do not intend to use the return value, use array_walk(). Commented Apr 25, 2024 at 4:23

2 Answers 2

4
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

from http://php.net/manual/en/function.implode.php

would be more efficient

Sign up to request clarification or add additional context in comments.

4 Comments

But is it more efficient?
In terms of the code, it's easier to follow. Why overcomplicate things? In terms of performance, any difference will be measured in microseconds I expect.
from what i can gather across the interblargs, implode would be the winner by at LEAST 1ms, if you need to arraymap to clean your data, then use array_walk and get on with your life @Sed
@ProEvilz From the theoretical point of view, it is much more efficient. It's only one C function call vs. a lot of interpreted code (the foreach version) or, even worse, a lot of PHP function calls (the array_map() version). Even more, implode() internally generates only one string of the final size and copies into it the values of the array. The other versions do a lot of string allocations, copying and deallocations. From the practical point of view, if the array doesn't contain thousands of items, the difference is negligible. But the code is much cleaner using implode().
3

You can also use implode(',',$array_of_ids). Just make sure you trim your values before using the implode.

$ids = ['foo','bar','boo'];
$ids = array_map('trim',$ids);
$ids_list = implode(',',$ids);
echo $ids_list; # foo,bar,boo

1 Comment

While the call to trim() is useful to produce a nice string, it doesn't add any value in this context. If the string generated this way is used to construct a IN (...) fragment of an SQL query, the whitespace do not matter. But with or without trim(), the answer is correct, nevertheless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.