ja_blackburn Posted January 20, 2011 Share Posted January 20, 2011 Hi. I have two arrays: $one = array('a', 'b', 'c'); $two = array('123', '456', '789'); My desired output is: $output = array('123a', '123b', '123c', '456a', '456b', '456c', '789a', '789b', '789c' ) How can this be achieved, I have looked into functions such as array_merge() but it doesnt seem to do the job. Help greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/225109-merge-array-question/ Share on other sites More sharing options...
BlueSkyIS Posted January 20, 2011 Share Posted January 20, 2011 one way. $one = array('a', 'b', 'c'); $two = array('123', '456', '789'); $count = min(count($one),count($two)); $output = array(); for ($i=0;$i<$count;$i++) { $output[] = $two[$i] . $one[$i]; } print_r($output); Link to comment https://forums.phpfreaks.com/topic/225109-merge-array-question/#findComment-1162685 Share on other sites More sharing options...
ja_blackburn Posted January 20, 2011 Author Share Posted January 20, 2011 Hi Thanks for that. The output i'm getting is: Array ( [0] => 123a [1] => 456b [2] => 789c ) So its not quite: $output = array('123a', '123b', '123c', '456a', '456b', '456c', '789a', '789b', '789c' ) Any Ideas on how I can get it apply each item from $one to $two in consecutive groups of three? Cheers. Link to comment https://forums.phpfreaks.com/topic/225109-merge-array-question/#findComment-1162703 Share on other sites More sharing options...
BlueSkyIS Posted January 20, 2011 Share Posted January 20, 2011 sorry, i did not look at the example close enough $one = array('a', 'b', 'c'); $two = array('123', '456', '789'); $output = array(); foreach ($two AS $val1) { foreach ($one AS $val2) { $output[] = $val1 . $val2; } } print_r($output); Link to comment https://forums.phpfreaks.com/topic/225109-merge-array-question/#findComment-1162779 Share on other sites More sharing options...
ja_blackburn Posted January 21, 2011 Author Share Posted January 21, 2011 Thanks very much for your help. Link to comment https://forums.phpfreaks.com/topic/225109-merge-array-question/#findComment-1162881 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.