[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

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!
0
Caruso_eu
Asked:
Caruso_eu
  • 3
  • 3
1 Solution
 
gr8gonzoConsultantCommented:
$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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now