Demonic Posted July 27, 2008 Share Posted July 27, 2008 Has anyone tried to make a function which merges more then two arrays? I know there is array_merge, but thats only for 2 arrays, wondering whats a quick way to merge a lot of arrays together? Quote Link to comment https://forums.phpfreaks.com/topic/116899-solved-has-anyone-made-a-function-to-merge-more-then-2-arrays/ Share on other sites More sharing options...
.josh Posted July 27, 2008 Share Posted July 27, 2008 well if nothing else, you could just loop the array merge...guess it kinda depends on the naming convention of your arrays. Quote Link to comment https://forums.phpfreaks.com/topic/116899-solved-has-anyone-made-a-function-to-merge-more-then-2-arrays/#findComment-601143 Share on other sites More sharing options...
Demonic Posted July 27, 2008 Author Share Posted July 27, 2008 well if nothing else, you could just loop the array merge...guess it kinda depends on the naming convention of your arrays. I haven't tried it yet, but I just pieced something like this together: function array_merge_alot() { $numargs = func_num_args(); $arg_list = func_get_args(); if( $numargs <= 1 ) { return 0; } elseif( $numargs == 2 ) { return array_merge( $arg_list[0], $arg_list[1] ); } else { $array = array_merge( $arg_list[0], $arg_list[1] ); for( $x = 3; $x < $num_args - 2; $x++ ) { $array = array_merge( $array, $arg_list[ $x ] ); } return $array; } } Wouldn't it be: for( $x = 3; $x < $num_args+1; $x++ ) { Quote Link to comment https://forums.phpfreaks.com/topic/116899-solved-has-anyone-made-a-function-to-merge-more-then-2-arrays/#findComment-601148 Share on other sites More sharing options...
Barand Posted July 27, 2008 Share Posted July 27, 2008 the manual is a wonderful thing <?php $ar1 = array (1,2,3); $ar2 = array (4,5,6); $ar3 = array (7,8,9); $ar4 = array ('a','b','c'); $array = array_merge($ar1, $ar2, $ar3, $ar4); echo '<pre>', print_r($array, true), '</pre>'; ?> --> Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => a [10] => b [11] => c ) Quote Link to comment https://forums.phpfreaks.com/topic/116899-solved-has-anyone-made-a-function-to-merge-more-then-2-arrays/#findComment-601149 Share on other sites More sharing options...
Demonic Posted July 27, 2008 Author Share Posted July 27, 2008 the manual is a wonderful thing <?php $ar1 = array (1,2,3); $ar2 = array (4,5,6); $ar3 = array (7,8,9); $ar4 = array ('a','b','c'); $array = array_merge($ar1, $ar2, $ar3, $ar4); echo '<pre>', print_r($array, true), '</pre>'; ?> --> Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => a [10] => b [11] => c ) -.- I thought it was a limit to only 2, ah well thanks. Quote Link to comment https://forums.phpfreaks.com/topic/116899-solved-has-anyone-made-a-function-to-merge-more-then-2-arrays/#findComment-601150 Share on other sites More sharing options...
Barand Posted July 27, 2008 Share Posted July 27, 2008 syntax from manual showing optional additional arrays array array_merge ( array array1 [, array array2 [, array ...]] ) Quote Link to comment https://forums.phpfreaks.com/topic/116899-solved-has-anyone-made-a-function-to-merge-more-then-2-arrays/#findComment-601160 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.