0

I'm having a few issues with foreach loops in PHP.

I have an outer foreach loop and an inner foreach loop.

The basics of the loops are, I have some posted data from a form, and some data from a db. The outer loop, loops through each post data and then compares this with data from the database inner loop.

The issue I have is, upon each outer loop, if an entry is found on the inner loop, how does the outer loop know its been found and then not repeat the entry.

So, here's the code I am using, i've commented so you can see the problem im having with the logic

        // get posted data      
    foreach($posted_vals as $post_key => $post_value) {

        // query all data in assignments table      
        foreach($existing_ids as $db_key => $db_value) {

            // check if user_id($db_value) matches ($post_value), 
            // if not then post to db

            // if this loop does not find matching data and
            // posts to the database, how does the outer loop
            // know its been found and then not post again to database

        } 

        // any entries not found in db, post to db ... er but hang on,
        // how do i know if any entries were posted???

    } 
2
  • Add a logical test and then use break if the test is true Commented Jan 9, 2016 at 17:01
  • How can i test if a value appears in both associative arrays? if($posted_vals[user_id]==$existing_ids[user_id]) . I cant seem to compare outside of the inner loop??!? Commented Jan 9, 2016 at 17:07

2 Answers 2

1

you can set a flag variable isFound whether the post were found in inner loop or not. Initially the variable is false but if the post is found in inner loop it will be updated to true then in outer loop you can check if the isFound is true or fale. Thus you can check whether your post was found or not in inner loop. Here is how you can do it:

// get posted data      
foreach($posted_vals as $post_key => $post_value) {
    $isFound = false;
    // query all data in assignments table      
    foreach($existing_ids as $db_key => $db_value) {
      //if found in db then set $isFound = true;
    } 
    //if(!isFound) that means if the post was not found
    // any entries not found in db, post to db ... er but hang on,
    // how do i know if any entries were posted???
} 
Sign up to request clarification or add additional context in comments.

Comments

1

It might become more clear when you put the db-comparison in a separate function:

foreach($posted_vals as $post_key => $post_value) {
    if(compareWithDbValues($post_value)) {
        return true;
    }
}

function compareWithDbValues($post_value) {
    foreach($existing_ids as $db_key => $db_value) {
        if($db_value == §post_value) {
            return true;
        }
    }

    return false;
}

If you do not have it in a separate function just use break instead of return true in the inner for loop.

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.