0

Lets say I have two arrays like this:

$array1(one,  two, three);
$array2(four, five, six);

And I want the result to be like this:

[0] -> one four
[1] -> two five
[2] -> three six

How should I do this?

Kimmo
  • 67
  • 1
  • 7

2 Answers2

2

You can use array_map function providing your two arrays in parameter :

<?php
$array1=Array("one",  "two", "three");
$array2=Array("four", "five", "six");

$res=array_map(function($r1, $r2) {return "$r1 $r2";}, $array1, $array2);
print_r($res);

Result

Array
(
    [0] => one four
    [1] => two five
    [2] => three six
)
Adam
  • 17,838
  • 32
  • 54
  • Yes, array_map seems to work exactly as I wanted. For some reason I did not find this on PHP manual. Only combine, merge and so on. Thank you for the quick answer! – Kimmo Feb 28 '15 at 11:03
1

try this

function array_interlace() { 
$args = func_get_args(); 
$total = count($args); 

if($total < 2) { 
    return FALSE; 
} 

$i = 0; 
$j = 0; 
$arr = array(); 

foreach($args as $arg) { 
    foreach($arg as $v) { 
        $arr[$j] = $v; 
        $j += $total; 
    } 

    $i++; 
    $j = $i; 
} 

ksort($arr); 
return array_values($arr); 
}
$array1=array('one',  'two', 'three');
$array2=array('four', 'five',' six');
print_r(array_interlace($array1,$array2));
priya786
  • 1,804
  • 12
  • 11