xProteuSx Posted December 22, 2011 Share Posted December 22, 2011 I have two arrays, as follows: $array1 = (1, 2, 3); $array2 = (4, 5, 6); Is there a function to combine the two without having to do a loop? I'd like to create: $array3 = (1, 2, 3, 4, 5, 6); Link to comment https://forums.phpfreaks.com/topic/253662-merge-2-arrays-to-make-1/ Share on other sites More sharing options...
Philip Posted December 22, 2011 Share Posted December 22, 2011 Yup, surprisingly named array_merge Link to comment https://forums.phpfreaks.com/topic/253662-merge-2-arrays-to-make-1/#findComment-1300403 Share on other sites More sharing options...
xProteuSx Posted December 22, 2011 Author Share Posted December 22, 2011 Surprisingly the documentation for this function is confusing to a newbie. I have tried: $array3 = array_merge($array1, $array2); I have also tried: $array1 = array_merge($array1, $array2); Neither has worked. Link to comment https://forums.phpfreaks.com/topic/253662-merge-2-arrays-to-make-1/#findComment-1300406 Share on other sites More sharing options...
harristweed Posted December 22, 2011 Share Posted December 22, 2011 <?php $array1 = array(1, 2, 3); $array2 = array(4, 5, 6); $array3=array_merge($array1, $array2); foreach ($array3 as $key => $value) { echo"key = $key value = $value<br />\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/253662-merge-2-arrays-to-make-1/#findComment-1300409 Share on other sites More sharing options...
xProteuSx Posted December 22, 2011 Author Share Posted December 22, 2011 Hmmm ... I guess that the following worked: $array3 = array_merge($array1, $array2); Wonder why I didn't catch that. Thanks. Link to comment https://forums.phpfreaks.com/topic/253662-merge-2-arrays-to-make-1/#findComment-1300412 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.