PHP combining arrays - little complicated

Posted on 2012-08-21
Last Modified: 2012-08-21

i have the following problem.

Lets say we have 2 arrays.

Array one looks like this:  Array_1 = [va1] [va2] [val3]
and arry2 is :                     Array_2 = [ins_1][ ins_2][ ins_3][ ins_4][ ins_5][ ins_6]

I want to combine those two into 3rd array which should look like this:

Array_3 = [[va1] [va2] [val3] [ins_1]]  [[va1] [va2] [val3] [ins_2]]  [ [va1] [va2] [val3] [ins_3]]  [[va1] [va2] [val3] [ins_4]] …….
Anyone got an idea how to pull it off?

thank you!
Question by:Caruso_eu
    LVL 34

    Accepted Solution

    $arr1 = array("va1","va2","va3");
    $arr2 = array("ins_1","ins_2","ins_3","ins_4","ins_5","ins_6");

    $arr3 = array();
    foreach($arr2 as $instance)
       $newarray = $arr1;
       $newarray[] = $instance;
       $arr3[] = $newarray;
    LVL 49

    Expert Comment

    by:Julian Hansen
    $a = array(1,2,3);
    $b = array('a','b','c');
    $new = array();
    foreach($b as $i)
       $x = $a;
       $x[] = $i;
       $new[] = $x;
    echo "<pre>";
    echo "</pre>";

    Open in new window

    LVL 34

    Expert Comment

    No offense, julian, but that's my exact code that you just reposted but with different variable names.
    LVL 49

    Expert Comment

    by:Julian Hansen
    @gr8gonzo no offence taken except I posted that code before I saw your solution.

    i.e. we both went to the question
    you typed in yours and posted
    I typed in mine and posted
    mine arrived after yours

    LVL 34

    Expert Comment

    No worries.

    Author Comment

    Since both answers are identical i have rewarded the one who posted first.

    Excellent forum if i might add ... i am subscribing to it as we speak!
    LVL 49

    Expert Comment

    by:Julian Hansen
    @Caruso_eu - correct decision.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now