-5

I have two arrays:

$arr1= array("A","B","C");
$arr2= array("1","2","3");

Output i need as:

$arr3= array("A","1","B","2","C","3");

Can anyone help please?

1
  • 5
    Show us what you have tried so far. Commented Dec 17, 2015 at 10:09

4 Answers 4

0

If your first two arrays has the same length, you can use a loop to get the array you want:

<?PHP
$arr1= array("A","B","C");
$arr2= array("1","2","3");
$arr3=[];
for($i = 0; $i < count($arr1); $i++)
    array_push($arr3, $arr1[$i], $arr2[$i]);
?>

It will return:

$arr3= array("A","1","B","2","C","3");

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

1 Comment

Thanks for the answer & not like others who mind answering any easy question.
0

Take a look at array_merge()

array_merge ( array $array1 [, array $... ] )

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Comments

0

This will work to combine the two arrays together:

$output = $array1 + $array2;

Comments

0

This snippet could solve what you asked, also if arrays are not equal in length.

function array_interpolation($arr1, $arr2) {
    $result = array();
    $len1 = count($arr1);
    $len2 = count($arr2);
    $maxlen = max($len1, $len2);
    for($i = 0; $i < $maxlen; $i++) {
        if($i < $len1) {
            array_push($result, $arr1[$i]);
        }
        if($i < $len2) {
            array_push($result, $arr2[$i]);
        }
    }
    return $result;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.