0

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

0

1 Answer 1

2

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.