4

I m trying to create a third array of numbers by showing the 4 first elements from first array and 1 first elements from the second array and so on.

Example:

A1 { 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 21, 22, 23, 24, 25 }
A2 {11, 12, 13, 14, 15, 16, 17, 18, 19, 20 } 

And the result should be:

A3 { 1, 2, 3, 4, 11, 5, 6, 7, 8, 12, 9, 10, 21, 22, 13, ... }
     └───┬────┘  ↑↑  └───┬────┘  ↑↑  └─────┬─────┘  ↑↑
         A1      A2      A1      A2        A1       A2 

Right now I tried this code, but it doesn't show all elements from the second array.

<?php

    $array = array(1, 2, 3, 4, 5,6,7,8,9,10);
    $array2 = array(11, 12, 13, 14, 15 ,16,17,18,19,20);

    $temp = 0;

    foreach ($array as $key => $item)
    {
        echo $item;

        if(($key+1) % 4 == '0')
        {
            echo $array2[$temp];
            $temp++;
        } 
    }


?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Jass
  • 41
  • 3
  • After raking 22 from A!, you're left with A1 (23, 24, 25) and A2 (14,15,16,17,18,19,20). So, how's it supposed to work now? Since A1 has only 3 elements left. – Indrasis Datta Mar 14 '16 at 04:48

4 Answers4

2

Explanation

Here I first chunk the first array in parts of 4 elements each and the second one in parts of 1 with array_chunk(), like this:

//Array one
Array (
    Array (
        //4 array elements
    )
    //...
)

//Array two
Array (
    Array (
        //1 array elements
    )
    //...
)

Then you can loop through both arrays at once with array_map() and merge both arrays together with array_merge(). Since there might be an uneven amount of elements from both arrays, I simply check with is_array() if we still have an array with elements to merge or if we have to use an empty array for array_merge().

So after array_map() we end up with a two-dimensional array, each subArray containing 4 elements from the first array and 1 from the second, like this:

//Array
Array (
    Array (
        //4 array elements from array one
        //1 array elements from array two
    )
    //...
)

And at then end we can just use call_user_func_array() combined with array_merge() to get all elements as one-dimensional array.

Code

<?php

    $array = array(1, 2, 3, 4, 5,6,7,8,9,10);
    $array2 = array(11, 12, 13, 14, 15 ,16,17,18,19,20);
    
    $result = call_user_func_array("array_merge", array_map(function($arrayOne, $arrayTwo){
        return array_merge(is_array($arrayOne) ? $arrayOne : [], is_array($arrayTwo) ? $arrayTwo : []);
    }, array_chunk($array, 4), array_chunk($array2, 1)));
    
    
    print_r($result);

?>

output:

Array (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 11
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 12
    [10] => 9
    [11] => 10
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [19] => 20
)
Community
  • 1
  • 1
Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

Try creating a third array, and pushing each item of the other two into it (using your logic for determining the new item order)

<?php
    $array = array(1,2,3,4,5,6,7,8,9,10,21,22,23,24,25);
    $array2 = array(11, 12, 13, 14, 15 ,16,17,18,19,20);
    $array3=array();
    $temp=0;

    foreach ($array as $key => $item)
      {
        array_push($array3,$item);
        if(($key+1)%4=='0')
           {
               array_push($array3,$array2[$temp]);
               $temp++;
            } 
       }

     $count=count($array2);
     if($temp != $count)
         {
             for($a=$temp;$a<$count;$a++)
             {
                array_push($array3,$array2[$a]);
             } 
         }
    ?>

this gives $array3 the contents of:

1, 2, 3, 4, 11, 5, 6, 7, 8, 12, 9, 10, 21, 22, 13, 23, 24, 25, 14, 15, 16, 17, 18, 19, 20

which is all array1, in four digits sets, interspersed with the first 3 elements of array 2 and then the remaining elements of array2. As expected.

gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • As OP wrote *But it not Shows complete elent of array2* I think he wants all the elements from array two in the result, just at the end. I think you should be able to modify your code by using some more logic with if statements and check if the other array still has elements in it and if not just push the elements. – Rizier123 Mar 14 '16 at 05:13
0

According to your code, the loop will end once it reaches the end of first array ($array). It will not show the full elements of second array if the second array contains more number of elements than 1/4th of the first array.

Make the code print the remaining elements of each array

$array = array(1, 2, 3, 4, 5,6,7,8,9,10);
$array2 = array(11, 12, 13, 14, 15 ,16,17,18,19,20);

$temp = 0;
$length = count($array2);

foreach ($array as $key => $item){
    echo $item;
    if(($key+1)%4 == '0' && $temp < $length){
        echo $array2[$temp];
        $temp++;
    } 
}

// Print remaining elements if there are any
for ($i = $temp; $i < $length; $i++) {
    echo $array2[$i];
}
gmuraleekrishna
  • 3,375
  • 1
  • 27
  • 45
Syamesh K
  • 826
  • 8
  • 18
0

use isset or array_key_exists() to check array index exist and store new array

$new = array();
foreach ($array as $key => $item)
{
    if(($key+1) % 4 == 0)
    {
        $new[] = $item;
        isset($array2[$temp]) ? $new[] = $array2[$temp] : "";//isset is check array index is exist in array 2
        $temp++;
    } 
    else{
        $new[] = $item;
    }
}
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52