Combining Arrays of different length in PHP

PHP 5.2.6
I'm just learning programming and this is a loooooong stretch for me. Any help is appreciated.
I have two arrays generated from a html form.  The content of the array is dynamic based on which options the user checks in  the from. The end result needs to be placed as a record in a MySQL DB.
can anyone point me in a direction on how  I go about combining the arrays such that I get the result I'm lookiing for.

$array1 = ('04023','1'),('04023','5'),('04023','12')
$array2 =('04023','0101'),('04023','0102'),('04023','0501'),('04023','0505'),('04023','0509'),(04023','1203')
Result needs to be:
$array3 = ('04023','1','0101'),('04023','1','0102'),('04023','5','0501'),('04023','5','0505')('04023','5','0509'),('04023','12',1203)
shootinstr8Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
$array3= array_merge( $array1, $array2);
0
hieloCommented:
0
shootinstr8Author Commented:
hielo, thanks for commenting!
I tried that already. I got
array3 =>([0]=>(('04023','1'),('04023','5'),('04023','12')) , [1]=>(('04023','0101'),('04023','0102'),('04023','0501'),('04023','0505')))..etc...
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

spoxoxCommented:
You can generate the necessary array3 as shown below. If you're supposed to consult array1, the process will end up being the same - but more complicated.



<?php
$array1 = array(array('04023','1'), array('04023','5'), array('04023','12'));
$array2 = array(array('04023','0101'), array('04023','0102'), array('04023','0501'), array('04023','0505'), array('04023','0509'), array('04023','1203'));
 
for ($i = 0; $i < sizeof($array2); $i++) {
  $fld2 = substr($array2[$i][1],0,2);
  if (substr($fld2,0,1) == '0') 
    $fld2 = substr($fld2,1);
  
  $arrayTemp = array($array2[$i][0], $fld2, $array2[$i][1]);
  $array3[$i] = $arrayTemp;
}
?>

Open in new window

0
spoxoxCommented:
To to the MySQL inserts, you'll need something like this snippet (edited for your MySQL table, columns, ID, PW, DBname, ...)


$username = "ee";
$password = "eepw";
$hostname = "localhost";    
$dbconn = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
 
$database = mysql_select_db("eedb",$dbconn);
 
 
for ($i = 0; $i < sizeof($array3); $i++) {
  $query = "INSERT INTO someTable (fldName1, fldName2, fldName3) VALUES (";
 
  for ($j = 0; $j < sizeof($array3[$i]); $j++) {
    if ($j+1 == sizeof($array3[$i])) {
      $query .= "'" . $array3[$i][$j] . "')";
    } else {
      $query .= "'" . $array3[$i][$j] . "',";
    }  
  }
  
  // Perform Query
  $result = mysql_query($query);
  if (!$result) {
      $message  = 'Invalid query: ' . mysql_error() . "\n";
      $message .= 'Whole query: ' . $query;
      die($message);
  }
 
}
 
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hieloCommented:
try:
$array3=array();
foreach($array1 as $sub1)
{
	foreach($array2 as $sub2)
	{
		if( (int)$sub1[1] == (int)substr($sub2[2],0,2) )
		{
			$array3[]=array_merge($sub1,array($sub2[2]));
		}
	}
}

Open in new window

0
shootinstr8Author Commented:
Thanks  for commenting spoxox;
WOW! I'm going to need some time to digest what you just did! I've tried your first post and everything printed as you said it would. I'm working on  imploding the array so I can insert with one statement.
I don't quite understand your second post yet. If I stare long enough maybe it will make sense! drool,drool ;)  
0
shootinstr8Author Commented:
hielo, I tried your second post. I get  "Array();" as a result. do I need to modify the script?

<?php
$array1 = array(array('04023','1'), array('04023','5'), array('04023','12'));
$array2 = array(array('04023','0101'), array('04023','0102'), array('04023','0501'), array('04023','0505'), array('04023','0509'), array('04023','1203'));
 

 $array3=array();
foreach($array1 as $sub1)
{
      foreach($array2 as $sub2)
      {
            if( (int)$sub1[1] == (int)substr($sub2[2],0,2) )
            {
                  $array3[]=array_merge($sub1,array($sub2[2]));
            }
      }
}
 print_r($array3);
?>
0
hieloCommented:
sorry. It should have been $sub2[1] not $sub2[2]. (I was staring at $array3 of what you posted! )
...
            if( (int)$sub1[1] == (int)substr($sub2[1],0,2) )
            {
                  $array3[]=array_merge($sub1,array($sub2[1]));
            }
...

Open in new window

0
spoxoxCommented:
Explanation of post 22964054:

  for ($j = 0; $j < sizeof($array3[$i]); $j++) {
    if ($j+1 == sizeof($array3[$i])) {
      $query .= "'" . $array3[$i][$j] . "')";
    } else {
      $query .= "'" . $array3[$i][$j] . "',";
    }  
  }

$array3 (and the other arrays) are arrays of arrays; in other words, $array[0] refers to an array (with these elements:  ('04023','1','0101')  ).

Each SQL INSERT statement will look something like this:
INSERT INTO aTable(col1, col2, col3) VALUES ('04023','1','0101')

'04023' can be referenced as $array3[0][0] (i.e., the first element of the first array, in the collection of arrays referenced by $array3). Similarly, '1' is $array[0][1].

The {for $i} loop goes through each of the arrays in $array3.
The {for $j} loop goes through each element in each of $array3's arrays.
The {for $j} IF statement just adds the commas between elements; or, if at the end of the array, it adds the closing parenthesis.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

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.