t_machine Posted May 14, 2009 Share Posted May 14, 2009 I hope someone can help with this problem. I have an array with many values and would like to implode a " - " after every 4th number. Example 1234-5678-91011... My array: array(1, 2, 3, 4, 5, 6.... How can i use implode to add the " - " Thanks for any help Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 14, 2009 Share Posted May 14, 2009 I think the easiest way (not the shortest) would be to loop through the array and add the numbers to a string as you go along. You'll probably want to keep track of a number so you'll know to add the hyphen when the 4th number comes up. Quote Link to comment Share on other sites More sharing options...
Philip Posted May 14, 2009 Share Posted May 14, 2009 $original = array(1,2,3,4,5,6,7,8,9,0); $temp = array(); foreach(array_chunk($original, 4) as $k=>$v) $temp[] = implode($v); echo implode('-', $temp); Displays: 1234-5678-90 Quote Link to comment 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.