Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:07:58+00:00 2026-06-09T03:07:58+00:00

Suppose I have two arrays: $a1 = array(0, 1, 2); $a2 = array(3, 4,

  • 0

Suppose I have two arrays:

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

I want to be able to do a merge technique that alternates the array values and not just concatenate them. I want this result:

array(0, 3, 1, 4, 2, 5);

Is there a native way to do this as performance is an issue here since I need to do this thousands of times

Please note, I know I can do it like this:

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

I’m looking for a built in way if there is a faster one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:08:00+00:00Added an answer on June 9, 2026 at 3:08 am
    $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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two arrays like the following: $arr2 = array(array(first, second), array(third, fourth));
Suppose I have two arrays: val ar1 = Array[String](1, 2, 3) val ar2 =
Suppose we have two arrays: $a=array('1'=>'Apple','2'=>'Microsoft', '3'=>'Microapple','4'=>'Applesoft','5'=>'Softapple'); $b=array(1,3); Where $b array represents the keys
Supposed that I have two arrays: Dim RoomName() As String = {(RoomA), (RoomB), (RoomC),
Suppose I have two h:inputText components. I want to bind both of the text
suppose that we have three array int a[]=new int[]{4,6,8,9,11,12}; int b[]=new int[]{3,5,7,13,14}; int c[]=new
supposing to have two arrays a[N],b[N] containing only 0 and 1 values, is there
suppose you are given the following problem. You have two index sets that have
Suppose I have two arrays: a = [1, 2, 3, 4] b = [5,
Suppose we have an object that can be sorted using two or more comparison

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.