gpt4 book ai didi

PHP 退出内部 foreach 循环并继续执行外部循环

转载 作者:行者123 更新时间:2023-11-29 07:25:34 25 4
gpt4 key购买 nike

我在 PHP 中的 foreach 循环方面遇到了一些问题。

我有一个外部 foreach 循环和一个内部 foreach 循环。

循环的基础知识是,我有一些来自表单的发布数据和一些来自数据库的数据。外循环遍历每个帖子数据,然后将其与数据库内循环中的数据进行比较。

我遇到的问题是,在每个外循环上,如果在内循环上找到一个条目,外循环如何知道它已找到,然后不重复该条目。

所以,这是我正在使用的代码,我已经发表了评论,以便您可以看到我在逻辑上遇到的问题

        // 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???

}

最佳答案

您可以设置一个标志变量isFound,无论该帖子是否在内循环中找到。最初该变量为 false 但如果在内部循环中找到该帖子,它将更新为 true 然后在外部循环中您可以检查是否 isFound > 是真是假。因此,您可以检查您的帖子是否在内循环中找到。以下是您可以如何做到的:

// 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???
}

关于PHP 退出内部 foreach 循环并继续执行外部循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34696316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com