TheBrandon Posted May 11, 2012 Share Posted May 11, 2012 Hello all, I'm trying to get better at manipulating arrays and I'm stumped on this one. What would be the most efficient way of converting an array such as this: Array ( [3] => 3 [9] => 1 [15] => 9 ) Into this: 3,3,3,9,15,15,15,15,15,15,15,15,15 Quote Link to comment https://forums.phpfreaks.com/topic/262381-help-converting-an-array-into-comma-separated-values/ Share on other sites More sharing options...
Pikachu2000 Posted May 11, 2012 Share Posted May 11, 2012 implode Quote Link to comment https://forums.phpfreaks.com/topic/262381-help-converting-an-array-into-comma-separated-values/#findComment-1344653 Share on other sites More sharing options...
scootstah Posted May 11, 2012 Share Posted May 11, 2012 It's probably not the most efficient way, but this works: $input = array('3' => 3, '9' => 1, '15' => 9); $str = ''; foreach($input as $key => $val) { for($i = 0; $i < $val; $i++) { $str .= $key . ','; } } $str = rtrim($str, ','); EDIT: This is better: $input = array('3' => 3, '9' => 1, '15' => 9); $str = ''; foreach($input as $key => $val) { $str .= str_repeat($key . ',', $val); } $str = rtrim($str, ','); Quote Link to comment https://forums.phpfreaks.com/topic/262381-help-converting-an-array-into-comma-separated-values/#findComment-1344654 Share on other sites More sharing options...
TheBrandon Posted May 11, 2012 Author Share Posted May 11, 2012 implode implode alone is capable of that? Could you show me an example of how to achieve this using implode? I've been trying to do it with a foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/262381-help-converting-an-array-into-comma-separated-values/#findComment-1344655 Share on other sites More sharing options...
TheBrandon Posted May 11, 2012 Author Share Posted May 11, 2012 It's probably not the most efficient way, but this works: $input = array('3' => 3, '9' => 1, '15' => 9); $str = ''; foreach($input as $key => $val) { for($i = 0; $i < $val; $i++) { $str .= $key . ','; } } $str = rtrim($str, ','); EDIT: This is better: $input = array('3' => 3, '9' => 1, '15' => 9); $str = ''; foreach($input as $key => $val) { $str .= str_repeat($key . ',', $val); } $str = rtrim($str, ','); Oh okay, the first one is similar to what I was doing. I'll look into str_repeat, thanks a lot! =) Quote Link to comment https://forums.phpfreaks.com/topic/262381-help-converting-an-array-into-comma-separated-values/#findComment-1344656 Share on other sites More sharing options...
xyph Posted May 11, 2012 Share Posted May 11, 2012 It's hard to improve much on scootstah's solution. This is an alternate, using str_repeat. It could be faster, but if it is, you'd have to run it a million times before it'd make a noticeable difference <?php $array = array(3=>3,9=>1,15=>9); $out = ''; foreach( $array as $val => $mult ) $out .= str_repeat("$val,", $mult); $out = substr($out,0,-1); echo $out; ?> [edit] Ninja'd by scoot's edit [/edit] Quote Link to comment https://forums.phpfreaks.com/topic/262381-help-converting-an-array-into-comma-separated-values/#findComment-1344658 Share on other sites More sharing options...
Pikachu2000 Posted May 11, 2012 Share Posted May 11, 2012 implode implode alone is capable of that? Could you show me an example of how to achieve this using implode? I've been trying to do it with a foreach loop. Oh, I guess I should have examined the values more closely when I saw there were more values than array elements. No, implode() will not do that on its own. Quote Link to comment https://forums.phpfreaks.com/topic/262381-help-converting-an-array-into-comma-separated-values/#findComment-1344661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.