Predrag Beocanin
Predrag Beocanin

Reputation: 1400

Insert multiple arrays

I'm trying to grab all values from my inputs and insert them together, so for each value #1 in first array, insert value #1 in second and third. This is kind of what it looks like:

$lines = explode(PHP_EOL, $_POST['links']);
$keywords = explode(PHP_EOL, $_POST['keywords']);
$violationtypes = explode(PHP_EOL, $_POST['keywords']);

Those inputs are regular, but they may be 1 or 500, I honestly don't know. I used to handle this while there was one input like this:

foreach($lines as $line)
    {
if (!empty($line))
    {
        if (false === strpos($line, '://')) 
        {
           $line = 'http://' . $line;
        }
mysql_query("INSERT INTO links (ClientEmail,Links) VALUES ('$Emailvalue', '$line')");
}
}

However, I can't pull this one with 3 arrays. Is there a better way?

PS The check for empty lines is so that I don't add empty values into the database, and the other one is checking if http:// is there, and adds it if its not. That check is only for $lines, other inputs don't need any check

Upvotes: 0

Views: 46

Answers (1)

Barmar
Barmar

Reputation: 782407

Use the index in one array to fetch the corresponding elements of the other arrays:

foreach ($lines as $i => $line) {
    if (!empty($line)) {
        $keyword = $keywords[$i];
        $violation = $violationtypes[$i];
        // Now insert $line, $keyword, and $violation into DB
    }
}

Upvotes: 2

Related Questions