Jump to content

[SOLVED] Has anyone made a function to merge more then 2 arrays?


Recommended Posts

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++ ) {

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
)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.