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 Link to comment https://forums.phpfreaks.com/topic/158075-how-to-implode-after-4th-value-in-array/ 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. Link to comment https://forums.phpfreaks.com/topic/158075-how-to-implode-after-4th-value-in-array/#findComment-833847 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 Link to comment https://forums.phpfreaks.com/topic/158075-how-to-implode-after-4th-value-in-array/#findComment-833858 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.