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?

2
  • No, it does not. But you can either use a foreach or iterate using next() on both input arrays for merging. Commented Nov 19, 2011 at 13:29
  • PHP has the MultipleIterator which comes close. Commented Jun 9, 2012 at 15:45

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Looks like way better code then my idea on how to do it.
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;
}

Comments

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;
}

Comments

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

Comments

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

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.