2

Is there a straightforward way in PHP to combine multiple arrays together but respect the relative order of the elements?

So for example if I had two arrays

$x = ('1','2','3','4');

and

$y = array('a','b','c','d','e');

and I wanted combine them into a single array

$z = ('1','a','2','b','3','c','4','d','e');

Is there a straightforward way of doing this? The ideal solution would also account for different lengths of arrays, behaving as my example does.

array_merge doesn't seem to achieve what I want as it just appends one array to the other.

My current solution loops through each array in order and pushes values to my $z array. This is not only inelegant, it also requires the input arrays to have the same number of values, which isn't ideal for my application.

Any insight would be appreciated

Jon R
  • 43
  • 4

4 Answers4

2

I think this should work -

$count = max(count($x), count($y));

$newArr = array();
for ($i = 0; $i < $count; $i++) {
  // enter values to the new array based on key
  foreach(array($x, $y) as $arr) {
    if (isset($arr[$i])) {
      $newArr[] = $arr[$i];
    }
  }
}

var_dump($newArr);
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
  • This doesn't work for a lot of reasons : no opening bracket after `foreach` but a closing one, `$newArr` declared as an `array` but used as a `string` in the loop, etc. Please test your code before posting. – roberto06 Nov 08 '16 at 13:05
  • Of course, the answer was edited as I was writing my comment... Dammit ! It works now, downvote removed. – roberto06 Nov 08 '16 at 13:06
  • The inner loop executes for two times per iteration of `$i`, This is same as we write a check for $x and $y separately. – jitendrapurohit Nov 08 '16 at 13:14
1

Iterate from 0 to the greater length of the two arrays. At each step, if array $x contains item at index, push it to final array $z. Do the same for array $y.

  • The Asker already was doing this *".... My current solution loops through each array in order and pushes values to my $z array"*. Can you indicate what better solution you have in mind? – trincot Nov 08 '16 at 13:05
  • He said he performs two loops, one for each array. My suggestion is to loop only once – Dan Neacșu Nov 08 '16 at 13:19
1

You could use this generic function, which can accept any number of arrays, and will output an array with the elements taken from the first "column" first, then from the second, etc:

function mix_merge(...$args) {
    return call_user_func_array('array_merge', 
        array_map(function($i) use ($args) { return array_column($args, $i); },
                  range(0, max(array_map('count', $args))-1)));
}


// Sample data:
$x = array('1','2','3','4');
$y = array('a','b','c','d','e');    
$z = array('A','B','C');    

$res = mix_merge($x, $y, $z);

Result array will be:

['1', 'a', 'A', '2', 'b', 'B', '3', 'c', 'C', '4', 'd', 'e']
trincot
  • 317,000
  • 35
  • 244
  • 286
0

You can also try this

$x = array('1','2','3','4');
$y = array('a','b','c','d','e');
foreach($y as $key=>$value){
    if($x[$key]){
     $z[] = $x[$key];
     $z[] =     $value;

    }else {
        $z[] = $value;      
    }
}
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44