Hi there, I am trying to do the following...

I am trying to merge two arrays in php in sequence. I have tried the array_merge() but no luck on that one.

Does anyone have any ideas on merging two arrays like a deck of cards? Such as:

$array1 = {1,3,5,7};
$array2 = {2,4,6,8};

And then merge the two arrays so that final array would be $final_array = {1,2,3,4,5,6,7,8};

Any ideas?

Thanks all.

    If these were the arrays...

    $array1 = {1,3,5,7};
    $array2 = {8,6,4,2};
    

    ...would you still want the same result, or would you want {1,8,3,6,5,4,7,2}?

      NogDog wrote:

      If these were the arrays...

      $array1 = {1,3,5,7};
      $array2 = {8,6,4,2};
      

      ...would you still want the same result, or would you want {1,8,3,6,5,4,7,2}?

      Thx for your reply, yep, thats all i want... {1,8,3,6,5,4,7,2}...

        <?php
        $a = array(1,2,3,4);
        $b = array('a','b','c','d');
        $c = array();
        while(($d = current($a)) !== false && ($e = current($b)) !== false)
        {
           $c[] = $d;
           $c[] = $e;
           next($a);
           next($b);
        }
        echo "<pre>" . print_r($c,1) . "<pre>";
        /* output:
        Array
        (
            [0] => 1
            [1] => a
            [2] => 2
            [3] => b
            [4] => 3
            [5] => c
            [6] => 4
            [7] => d
        )
        */
        

        Note that if the arrays are not the same size, this will ignore any "extra" values in the longer array.

          Why not just use array_merge()? I believe:

          $final = $array1 + $array2;
          

          would also do what you want, but I heard it's more memory intensive somewhere.

            Horizon88 wrote:

            Why not just use array_merge()? I believe:

            $final = $array1 + $array2;
            

            would also do what you want, but I heard it's more memory intensive somewhere.

            Because the OP did not want one array appended to the end of the other, but rather to be "interlaced" with it, so to speak, alternating one value from array 1 with one value from array 2.

              Excellent Nog!!!!!!!!!!!
              Thank you, THANK you, THANK YOU!!!!!!!!😃

                NogDog wrote:

                Because the OP did not want one array appended to the end of the other, but rather to be "interlaced" with it, so to speak, alternating one value from array 1 with one value from array 2.

                Ahh, okay. I wasn't sure what he meant, but as always you've grasped it perfectly 😛 I suppose that's why they pay you the big bucks.

                Also, horsewhip, don't forget to mark this resolved, please.

                  Thx for the heads up Horizon, how do I go about doing that? Do I just write 'resolved' here?
                  Was looking around for a 'resolved' button, but dont see anything.

                    LOL, need sleep, missed the link in tools. Thx all, this is a great place!!

                      Write a Reply...