3

array one: 1,3,5,7 array two: 2,4,6,8

the array i want would be 1,2,3,4,5,6,7,8

I'm just using numbers as examples. If it was just numbers i could merge and sort but they will be words. So maybe something like

array one: bob,a,awesome

array two: is,really,dude

should read: bob is a really awesome dude

Not really sure how to do this. Does PHP have something like this built in?

hakre
  • 193,403
  • 52
  • 435
  • 836
Keverw
  • 3,736
  • 7
  • 31
  • 54
  • No, it does not. But you can either use a `foreach` or iterate using `next()` on both input arrays for merging. – mario Nov 19 '11 at 13:29
  • PHP has the [`MultipleIterator`](http://hakre.wordpress.com/2012/04/05/iterating-over-multiple-iterators-at-once/) which comes close. – hakre Jun 09 '12 at 15:45

6 Answers6

7

You could write yourself a function like this:

function array_merge_alternating($array1, $array2) {
    if(count($array1) != count($array2)) {
        return false; // Arrays must be the same length
    }

    $mergedArray = array();

    while(count($array1) > 0) {
        $mergedArray[] = array_shift($array1);
        $mergedArray[] = array_shift($array2);
    }
    return $mergedArray;
}

This function expects two arrays with equal length and merges their values.

If you don't need your values in alternating order you can use array_merge. array_merge will append the second array to the first and will not do what you ask.

halfdan
  • 33,545
  • 8
  • 78
  • 87
3

Try this elegant solution

function array_alternate($array1, $array2)
{
    $result = Array();
    array_map(function($item1, $item2) use (&$result)
                {
                    $result[] = $item1;
                    $result[] = $item2;             
                }, $array1, $array2);
    return $result;
}
Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131
2

This solution works AND it doesn't matter if both arrays are different sizes/lengths:

function array_merge_alternating($array1, $array2)
{
  $mergedArray = array();

  while( count($array1) > 0 || count($array2) > 0 )
  {
    if ( count($array1) > 0 )
      $mergedArray[] = array_shift($array1);
    if ( count($array2) > 0 )
      $mergedArray[] = array_shift($array2);
  }
  return $mergedArray;
}
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
1

Try this function:

function arrayMergeX()
{
  $arrays = func_get_args();
  $arrayCount = count($arrays);

  if ( $arrayCount < 0 )
    throw new ErrorException('No arguments passed!');

  $resArr = array();

  $maxLength = count($arrays[0]);

  for ( $i=0; $i<$maxLength; $i+=($arrayCount-1) )
  {
    for ($j=0; $j<$arrayCount; $j++)
    {
      $resArr[] = $arrays[$j][$i];
    }
  }
  return $resArr;
}

var_dump( arrayMergeX(array(1,3,5,7), array(2,4,6,8)) );
var_dump( arrayMergeX(array('You', 'very'), array('are', 'intelligent.')) );
var_dump( arrayMergeX() );

It works with variable numbers of arrays!

Live on codepad.org: http://codepad.org/c6ZuldEO

ComFreek
  • 29,044
  • 18
  • 104
  • 156
1

if arrays contains numeric values only, you can use merge and sort the array.

<?php
    $a = array(1,3,5,7);
    $b = array(2,4,6,8);

    $merged_array = array_merge($a,$b);
    sort($merged,SORT_ASC);
?>

else use this solution.

<?php
    function my_merge($array1,$array2)
    {
        $newarray = array();
        foreach($array1 as $key => $val)
        {
            $newarray[] = $val;
            if(count($array2) > 0)
                $newarray[] = array_shift($array2)
        } 

        return $newarray;
    }

?>

hope this help

0

Expects both arrays to have the same length:

$result = array();

foreach ($array1 as $i => $elem) {
   array_push($result, $elem, $array2[$i]);
}

echo join(' ', $result);
deceze
  • 510,633
  • 85
  • 743
  • 889