'Iterate through string and array then move on to subsequent elements in a loop en php

I need help on a script that is supposed to help me insert articles from a csv file into the database (Image and title). but I am new to PHP.

My problem is that I want to make a loop that inserts the title of the article in a variable (No problem) as well as the table with several images then redo the loop by passing to the following elements.

I have already tried with a for loop but I would have to be able to count the number of images each time and I did not succeed.

here's my code:

 <?php 

foreach($items as $it) {
                
     $title = $it[2];
     $img = $it[4];

     $countimg = count($img);
     for ($i=0; $i < $countimg; $i++) { 
        try {
            $sql = "INSERT INTO fill_wp (title, image) VALUES (:title, :imgr)"; 
            $stmt = $conn->prepare($sql);
            $stmt->execute(array(":title" => $title, ":imgr" => $imgr));
            header("Location: delexists.php?ok=1");
            }
        
                catch(PDOException $exception){
                die('ERROR: ' . $exception->getMessage());
        
        }
     }
}

Here's var_dump:

    Title 1
(
    [0] => e8d753_4d28f4185e754761aaedee75b710f4b7~mv2.jpg
    [1] => e8d753_2b34ab0d9ae84c84a50ea7a71e3b17b2~mv2.jpg
)
Title 2
(
    [0] => e8d753_c28f96d0262b4d8cacffeb032d08a66e~mv2.jpg
    [1] => e8d753_12e415672b404c6db915a92e5870a5c0~mv2.jpg
)
Title 3
(
    [0] => e8d753_2e4cea0057b848b8b6ef6e443fcac19b~mv2.jpg
    [1] => e8d753_8a7af381ec334aa49f19424132e0461e~mv2.jpg
)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source