Merge Two Flat Indexed Arrays of Equal Size So That Values Are Pushed into the Result in an Alternating Fashion

Merge two flat indexed arrays of equal size so that values are pushed into the result in an alternating fashion

$count = count($a1);
for ($i = 0; $i < $count; $i++) {
$newArray[] = $a1[$i];
$newArray[] = $b1[$i];
}

My work here is done.

$a1 = array(0,1,2);
$a2 = array(3,4,5);

$start = microtime(TRUE);

for($t = 0; $t < 100000; $t++)
{
$newArray = array();
$count = count($a1);
for ($i = 0; $i < $count; $i++)
{
$newArray[] = $a1[$i];
$newArray[] = $a2[$i];
}
}
echo round(microtime(TRUE) - $start, 2); # 0.6

$a1 = array(0,1,2);
$a2 = array(3,4,5);

$start = microtime(TRUE);

for($t = 0; $t < 100000; $t++)
{
$newArray = array();
for ($i = 0; $i < count($a1); $i++)
{
$newArray[] = $a1[$i];
$newArray[] = $a2[$i];
}
}
echo round(microtime(TRUE) - $start, 2); # 0.85

So pre-counting array size will be ~1/4 [citation needed] (on freakin' 100.000 iterations you will gain 0.2 in total) faster. If you put count() inside loop, it will recount on every iteration. 1/4 seems to me a reasonably faster. If you are looking for compiled function, you can stop.

P.S. Benchmark is like bikini, it shows you everything, and nothing.

merge and reorder 3 arrays

First we create a multidimensional array with three original arrays, then we populate destination $result through two nested for loops:

$all    = [ $a, $b, $c ];
$result = [];
for( $i=0; $i<count( $a ); $i++ )
{
for( $j=0; $j<count( $all ); $j++ )
{
$result[] = $all[ 3*(($i+$j*2)%3>0)-(($i+$j*2)%3) ][ $i ];
}
}

eval.in demo

To populate $result, the second key [$i] is the progressive index of each original array (0, 1, 2, ...); to create first key we use this algorithm:

 3 * ( ($i + $j * 2 ) % 3 > 0 ) - ( ( $i + $j * 2 ) % 3 )       $i  $j
--------------------------------------------------------
3 * ( ( 0 + 0 * 2 ) % 3 > 0 ) - ( ( 0 + 0 * 2 ) % 3 ) 0 0
3 * ( ( 0 ) % 3 > 0 ) - ( ( 0 ) % 3 )
3 * ( 0 ) - ( 0 ) = 0

3 * ( ( 0 + 1 * 2 ) % 3 > 0 ) - ( ( 0 + 1 * 2 ) % 3 ) 0 1
3 * ( ( 2 ) % 3 > 0 ) - ( ( 2 ) % 3 )
3 * ( 1 ) - ( 2 ) = 1

3 * ( ( 0 + 2 * 2 ) % 3 > 0 ) - ( ( 0 + 2 * 2 ) % 3 ) 0 2
3 * ( ( 4 ) % 3 > 0 ) - ( ( 4 ) % 3 )
3 * ( 1 ) - ( 1 ) = 2
--------------------------------------------------------
3 * ( ( 1 + 0 * 2 ) % 3 > 0 ) - ( ( 1 + 0 * 2 ) % 3 ) 1 0
3 * ( ( 1 ) % 3 > 0 ) - ( ( 1 ) % 3 )
3 * ( 1 ) - ( 1 ) = 2

3 * ( ( 1 + 1 * 2 ) % 3 > 0 ) - ( ( 1 + 1 * 2 ) % 3 ) 1 1
3 * ( ( 3 ) % 3 > 0 ) - ( ( 3 ) % 3 )
3 * ( 0 ) - ( 0 ) = 0

3 * ( ( 1 + 2 * 2 ) % 3 > 0 ) - ( ( 1 + 2 * 2 ) % 3 ) 1 2
3 * ( ( 5 ) % 3 > 0 ) - ( ( 5 ) % 3 )
3 * ( 1 ) - ( 2 ) = 1
--------------------------------------------------------
3 * ( ( 2 + 0 * 2 ) % 3 > 0 ) - ( ( 2 + 0 * 2 ) % 3 ) 2 0
3 * ( ( 2 ) % 3 > 0 ) - ( ( 2 ) % 3 )
3 * ( 1 ) - ( 2 ) = 1

3 * ( ( 2 + 1 * 2 ) % 3 > 0 ) - ( ( 2 + 1 * 2 ) % 3 ) 2 1
3 * ( ( 4 ) % 3 > 0 ) - ( ( 4 ) % 3 )
3 * ( 1 ) - ( 1 ) = 2

3 * ( ( 2 + 2 * 2 ) % 3 > 0 ) - ( ( 2 + 2 * 2 ) % 3 ) 2 2
3 * ( ( 6 ) % 3 > 0 ) - ( ( 6 ) % 3 )
3 * ( 0 ) - ( 0 ) = 0
--------------------------------------------------------
3 * ( ( 3 + 0 * 2 ) % 3 > 0 ) - ( ( 3 + 0 * 2 ) % 3 ) 3 0
3 * ( ( 3 ) % 3 > 0 ) - ( ( 3 ) % 3 )
3 * ( 0 ) - ( 0 ) = 0

3 * ( ( 3 + 1 * 2 ) % 3 > 0 ) - ( ( 3 + 1 * 2 ) % 3 ) 3 1
3 * ( ( 5 ) % 3 > 0 ) - ( ( 5 ) % 3 )
3 * ( 1 ) - ( 2 ) = 1

3 * ( ( 3 + 2 * 2 ) % 3 > 0 ) - ( ( 3 + 2 * 2 ) % 3 ) 3 2
3 * ( ( 7 ) % 3 > 0 ) - ( ( 7 ) % 3 )
3 * ( 1 ) - ( 1 ) = 2
--------------------------------------------------------
(...)

Merge array values, alternating

You can use a for loop and refer to the index of each of the arrays you're combining.

$l = count($vars);
for ($i=0; $i < $l; $i++) {
$combined[] = $vars[$i];
$combined[] = $mods[$i];
}

In each iteration of the loop, you'll append one item from each of the original arrays. This will achieve the alternating effect.


As Steve pointed out, this can be done more simply using foreach:

foreach ($vars as $i => $value) {
$combined[] = $value;
$combined[] = $mods[$i];
}

How can I merge two associative arrays and preserve the global order of the entries?

Note that this solution will only work if the two arrays have the same length:

$arr1 = [ 'a' => '1', 'b' => 2 ];
$arr2 = [ 'h' => 'c', 'j' => '3' ];

$count = count($arr1);
$keys1 = array_keys($arr1);
$keys2 = array_keys($arr2);

$result = [];
for ($i = 0; $i < $count; $i++) {
$key1 = $keys1[$i];
$result[$key1] = $arr1[$key1];
$key2 = $keys2[$i];
$result[$key2] = $arr2[$key2];
}

print_r($result);

Output:

Array
(
[a] => 1
[h] => c
[b] => 2
[j] => 3
)

Edited based on mickmackusa's comment below.

Transpose and flatten multiple rows of array data

There is no PHP native function for this purpose. However according to the comment of @Mark Baker there is a short possibility to implement this:

$a = array("a","b","c");
$b = array("d","e","f");
$c = array("g","h","i");

function array_zip(...$arrays) {
return array_merge(...array_map(null, ...$arrays));
}

var_dump(array_zip($a,$b,$c));

Merge two arrays alternatively

assuming both arrays have the same length.

$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);

$new = array();
for ($i=0; $i<count($arr1); $i++) {
$new[] = $arr1[$i];
$new[] = $arr2[$i];
}
var_dump($new);

Merge Two Arrays so that the Values Alternate

You can use the map method:

var array1 = [1, 2, 3, 4, 5];var array2 = ['a', 'b', 'c', 'd', 'e'];
var arrayCombined = $.map(array1, function(v, i) { return [v, array2[i]];});
console.log(arrayCombined);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

How to interpolate two arrays?

I don't know if there is any built-in mapping function that makes this easier, but this is a simple, naive implementation.

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

function array_interpolate($a, $b) {
if (sizeof($a) != sizeof($b))
throw new Exception('Arrays must be of same size');

$result = array();
for ($i = 0, $l = sizeof($a); $i < $l; $i++) {
$result[2*$i] = $a[$i];
$result[2*$i+1] = $b[$i];
}
return $result;
}

$res = array_interpolate($a, $b);
print_r($res);

The above returns

Array
(
[0] => 1
[1] => a
[2] => 2
[3] => b
[4] => 3
[5] => c
[6] => 4
[7] => d
)

Hierarchical Array Conversion to Flat Array

$array = array(
array(1,2,3),
array('bob','bill'),
array(4,5,6,7)
);
function combinations($arrays) {
$result[] = array();
foreach ($arrays as $key => $values) {
$tmp = array();
foreach ($result as $result_item) {
foreach ($values as $value) {
$tmp[] = array_merge($result_item, array($key => $value));
}
}
$result = $tmp;
}

return $result;
}
$combinations_array = combinations($array);
$flat_string = '';
$flat_array = array();
foreach($combinations_array as $value){
foreach($value as $v){
$flat_string.=$v.',';
$flat_array[]=$v;
}
}

echo rtrim($flat_string,',');
print_r($flat_array);

Out Put string:
1,bob,4,1,bob,5,1,bob,6,1,bob,7,1,bill,4,1,bill,5,1,bill,6,1,bill,7,2,bob,4,2,bob,5,2,bob,6,2,bob,7,2,bill,4,2,bill,5,2,bill,6,2,bill,7,3,bob,4,3,bob,5,3,bob,6,3,bob,7,3,bill,4,3,bill,5,3,bill,6,3,bill,7

Output array:

    Array
(
[0] => 1
[1] => bob
[2] => 4
[3] => 1
[4] => bob
[5] => 5
[6] => 1
[7] => bob
[8] => 6
[9] => 1
[10] => bob
[11] => 7
[12] => 1
[13] => bill
[14] => 4
[15] => 1
[16] => bill
[17] => 5
[18] => 1
[19] => bill
[20] => 6
[21] => 1
[22] => bill
[23] => 7
[24] => 2
[25] => bob
[26] => 4
[27] => 2
[28] => bob
[29] => 5
[30] => 2
[31] => bob
[32] => 6
[33] => 2
[34] => bob
[35] => 7
[36] => 2
[37] => bill
[38] => 4
[39] => 2
[40] => bill
[41] => 5
[42] => 2
[43] => bill
[44] => 6
[45] => 2
[46] => bill
[47] => 7
[48] => 3
[49] => bob
[50] => 4
[51] => 3
[52] => bob
[53] => 5
[54] => 3
[55] => bob
[56] => 6
[57] => 3
[58] => bob
[59] => 7
[60] => 3
[61] => bill
[62] => 4
[63] => 3
[64] => bill
[65] => 5
[66] => 3
[67] => bill
[68] => 6
[69] => 3
[70] => bill
[71] => 7
)


Related Topics



Leave a reply



Submit