简体   繁体   中英

Removing an item from an array during a foreach loop

I have the following foreach being performed in PHP.

What I would like to do is instead of the $invalid_ids[] = $product_id; building and then looping around that, I would instead like to remove the entry from array that is being looped around as I'm looping around it..

For example:

If the current $product_id fails any of the test, delete the item from the $current_list array and proceed to the next iteration of the foreach loop.

I tried to do an unset($product_id) while the foreach loop header looked like this: foreach ($current_list as &$product_id) { , but the item item is still in the array.

Does anyone have any ideas on how I can go about doing this?

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;
    }
}

$product_id isn't the actual data in the array, it's a copy of it. You would need to unset the item from $current_list .

I'm don't know how $current_list is stored, but something like unset($current_list['current_item'] would do the trick. You can use key to select the current_item key in the array.

A similar way of iterating the Array, where you can get the array key, from the PHP key docs...

while ($fruit_name = current($array)) {
    if ($fruit_name == 'apple') {
        echo key($array).'<br />';
    }
    next($array);
}

Untested, but something like this...

while ($product_id = current($current_list)) {

    // Do your checks on $product_id, and if it needs deleting...
    $keyToDelete = key($array);
    unset($current_list[$keyToDelete]);

    next($current_list);
}

I think this simple code may help you

let's say we have an array of integers and we want to remove all the items that are equal to "2" inside of the foreach loop

$array = [1,2,1,2,1,2,1];
foreach ($array as $key => $value) 
{
   if($value==2)
      unset($array[$key]);
}
var_dump($array);

this shows the following result

array (size=4)
  0 => int 1
  2 => int 1
  4 => int 1
  6 => int 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM