PHP combining arrays - little complicated

Hello,

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!
Caruso_euAsked:
Who is Participating?
 
gr8gonzoConnect With a Mentor ConsultantCommented:
$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;
}
0
 
Julian HansenCommented:
<?php

$a = array(1,2,3);
$b = array('a','b','c');
$new = array();
foreach($b as $i)
{
   $x = $a;
   $x[] = $i;
   $new[] = $x;
}
echo "<pre>";
print_r($new);
echo "</pre>";
?>

Open in new window

0
 
gr8gonzoConsultantCommented:
No offense, julian, but that's my exact code that you just reposted but with different variable names.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Julian HansenCommented:
@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

Ok?
0
 
gr8gonzoConsultantCommented:
No worries.
0
 
Caruso_euAuthor Commented:
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!
0
 
Julian HansenCommented:
@Caruso_eu - correct decision.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.