Q:

Foreach through 2 arrays only loops the first element

I create 2 different arrays $postvalues and $filut from form inputs. I then send the arrays to my database table on button click. The arrays consist of file name and a small introduction.
for($i=0; $i<count($_FILES['upload']['name']); $i++) 
{
    $filut = basename($_FILES['upload']['name'][$i]);
}
if(isset($_POST['subi'])) 
{
     $data1 = $_POST['answers'];
     foreach($data1) as $postValues) 
     {
         $sql4 = 'INSERT INTO db_image (text, image) VALUES("'.$postValues.'","'.$filut.'")';
         $my->query($sql4);
     }
}    

The problem here is the output. The foreach loop outputs $postValues correctly but only duplicates $filut first element.
Example of the false output:
 this is picture1  picture1.png
 this is picture2  picture1.png  //Supposed to say 2nd element of $filut: "picture2.png"

Foreach goes through the first array properly but only duplicates the $filut array. I've tried searching for answer and I've tried combining the arrays but without any success.
How do I loop through both arrays properly and insert them to database?

A:

It sounds like you were trying to used nested loops, but you in fact had one loop at the beginning which closed out and then you loop through the rest. You need to move your closing curly brace marked below:
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
  $filut = basename($_FILES['upload']['name'][$i]);
 } <<<<<<<<<=========== This curly brace closes the "for loop"
if(isset($_POST['subi'])) {
 $data1 = $_POST['answers'];
 foreach($data1) as $postValues) {
 $sql4 = 'INSERT INTO db_image (text, image) VALUES("'.$postValues.'","'.$filut.'")';
 $my->query($sql4);
 }
} 
??? <<<<<======== For a nested loop you should close it here

or perhaps you were trying to assign $filut as an array in which case the starting for... loop needs to look like this:
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
  $filut[] = basename($_FILES['upload']['name'][$i]);
 }

(Note the addition of [] after $filut.)
However, if this is what you were intending (to make $filut into an array) then you need to do something different down below in order to access just one element of the array. This is where a nested foreach would solve the problem - maybe this is how you intended to finish it off:
if(isset($_POST['subi'])) {
    $data1 = $_POST['answers'];
    foreach($data1 as $postValues) {
        foreach ($filut as $f) {
            $sql4 = 'INSERT INTO db_image (text, image)
                     VALUES("'.$postValues.'","'.$f.'")';
            $my->query($sql4);
        }
    }
}

This will insert a row for every value in $filut for every value in $_POST['answers']. Is that what you wanted? Or are $filut and $_POST['answers'] in parallel and you want a 1-1 correlation on the insert?
Finally I'm going to suggest that you use prepare and bind since you are getting your data from the user. This example copied/modified from here:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Prepare an insert statement */
$query = "INSERT INTO db_image (text, image) VALUES (?,?)";
$stmt = $my->prepare($query);

#### NOTE THE USE OF YOUR VARIABLES HERE IN THE BIND - the "ss" means they are strings
$stmt->bind_param("ss", $postValues, $f);

#### HERE IS WHERE YOUR CODE IS GETTING INSERTED
foreach($data1 as $postValues) {
    foreach ($filut as $f) {
        /* Execute the statement */
        $stmt->execute();
    }
}

/* close statement */
$stmt->close();

/* close connection */
$my->close();
?>

I can't guarantee that I got all the variables changed to match your code, but hopefully it's close enough to give you an idea. This solution (1) protects you from SQL Injection (which is a serious flaw in your code otherwise) and (2) will be much faster by means of the prepare/execute cycle.
Do note the careful indentation in my examples where everything lines up perfectly with 3 or more spaces per indent - I can't tell you how many times I have seen sloppy (or too little) indentation make people miss problems which would be otherwise obvious.

