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! Quote Link to comment 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); Quote Link to comment 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. Quote Link to comment 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); Quote Link to comment 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. 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.