我在PHP中执行了以下foreach
。在foreach循环中删除数组中的项目
我想什么做的是代替$invalid_ids[] = $product_id;
建筑,然后周围的循环,我反而想从阵列移除,因为我周围循环正在被卷绕的卷绕入口..
例如:
如果当前$product_id
未通过任何测试,从$current_list
阵列删除的项并前进到foreach
循环的下一次迭代。
我试图做一个unset($product_id)
而foreach
环头是这样的:foreach ($current_list as &$product_id) {
,但该项目的项目仍然在数组中。
有没有人有任何想法,我该怎么做呢?
foreach ($current_list as $product_id) {
// Test 1 - Is the product still active?
// How to test? - Search for a product in the (only active) products table
$valid = $db->Execute("SELECT * FROM " . TABLE_PRODUCTS . " WHERE products_id = " . $product_id . " AND products_status = 1");
// Our line to check if this is okay.
if ($valid->RecordCount <= 0) { // We didn't find an active item.
$invalid_ids[] = $product_id;
}
// Test 2 - Is the product sold out?
if ($valid->fields['products_quantity'] <= 0 and STOCK_ALLOW_CHECKOUT == "false") { // We found a sold out item and it is not okay to checkout.
$invalid_ids[] = $product_id;
}
// Test 3 - Does the product have an image?
if (empty($valid->fields['products_image'])) { // Self explanatory.
$invalid_ids[] = $product_id;
}
}
'$ current_list'也是在功能初建的数组。但我会尽力回答你的答案。 –