Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

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)
0
shootinstr8
Asked:
shootinstr8
  • 4
  • 3
  • 3
1 Solution
 
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
Industry Leaders: 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!

 
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
 
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

Featured Post

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!

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