Multiple foreach statements

Hi All -

How do I handle using multiple foreach loops? Currently I have:

foreach($th as $t_file)
foreach ($orig as $file)

But this doesn’t seem correct to me…


You just need some more brackets:

foreach ($array as $value) {
    foreach ($array1 as $value1) {
        // do something with $value and $value1

Thanks very much –

I’m using it as an insert into a database:

foreach($value as $value1) {
foreach ($info as $info2)  {
		$sql = "INSERT INTO table_name (column1,column2) VALUES ('$value1','$info2')";
		if (!mysqli_query($link, $sql))
			$output = 'Error adding submitted path.';
			include 'output.html.php';

However, it gives 150 rows when only it should give 20 - since it repeats the entries when inserting into the database many times over.

What am I doing wrong here - any help is appreciated.


Can you post the code that is above (where your getting the contents of the $value and $info arrays from)?

Here is the code for ‘value1’. It is similar for ‘info’.

if ($handle = opendir('../thumbs/')) {
    while (false !== ($value1 = readdir($handle))) {
        if (($value1 != '.') && ($value1 != '..') && ($value1 != basename($_SERVER['PHP_SELF']))) {
			//store filename
			$value[] = $value1;


When I echo this out it gives me exactly what it should. And when I echo it into the database as one foreach statement, it works fine.
However, when I try to combine my foreach statements, it creates problems.

As SpacePhoenix said, having a look at some more code could come in handy. Anyway, if I understood it correctly, maybe you want to add a series of couples in the database, meaning a value from $value and a value from $info. Is this correct? If so, your code should be:

for (i=0; i<count($value); i++) {
    $sql = "INSERT INTO table_name (column1,column2) VALUES ('$value[i]','$info[i]')";

You get a lot of rows because what you wrote means that for every element in the first array you will insert a row in the database with every element of the second array -> if you have 10 elements in the first array and 10 elements in the second one, every element in the first array is inserted 10 times -> 100 rows.

I wouldnt even do that. Take a look at “Inserting Multiple Records Into a Table” in the MySQL manual, and you should be able to do 1 query to insert all your records, which would be much more efficient.

  (example_id, name, value, other_value)
  (100, 'Name 1', 'Value 1', 'Other 1'),
  (101, 'Name 2', 'Value 2', 'Other 2'),
  (102, 'Name 3', 'Value 3', 'Other 3'),
  (103, 'Name 4', 'Value 4', 'Other 4');

How are $value and $info related? If it’s a 1-to-1, you dont want a nested foreach at all. If it’s 1-to-all, you do.

I’ll take a look at that -

Yes, it is a one-to-one relation.
The data for both is coming from a readdir and then a converted thumbnail.


Okay - a 1-to-1 you should NOT need a nested foreach. Try something like this.

foreach($info AS $key => $info1) {
  $buildstring[] = "('".$info1."','".$value[$key]."')";
mysql_query("INSERT INTO table_name (column1,column2) VALUES " .implode(' , ',$buildstring).";");

This code assumes that $info[0] corresponds to $value[0], etc etc.

Unfortuantely, the above code doesn’t work - it generates 76 rows when there should only be 12.

I know there must be an answer here somewhere. Here is the simple code to insert with only one array [works great]:

$th[] = $t_file;

foreach($th as $t_file) {

	$sql = "INSERT INTO table_name (col_1) VALUES ('$t_file')";

Now, suppose I want to add another array [I know this isn’t correct]:

$th[] = $t_file;
$orig[] = $file;

foreach($th as $t_file) && ($orig as $file) {

	$sql = "INSERT INTO table_name (col_1,col_2) VALUES ('$t_file', '$file')";

How do I combine these arrays for an insert?

Okay, if you’re insistant on doing it one query at a time…

foreach($th AS $index => $t_file) {
    $sql = "INSERT INTO table_name (col_1,col_2) VALUES ('$t_file', '".$orig[$index]."')"; 
    //run query here.

again, this is assuming that $th[0] corresponds to $orig[0], etc.