I'm doing a For Each to loop through an array as you can see.
My Question is in each loop I'm running a check if it fails I want to delete this item from the array. How can I do this?

$oos = $_SESSION['outofstock'];
foreach ($oos as $row9) {


// do check then delete this one

}

    Take a look at the [man]array_filter[/man] function.

      I'm not sure how this removes an element of the array

      $oos = $_SESSION['outofstock'];
      foreach ($oos as $row9) {
      $NameCode = $row9['Desc'];
      
      // remove from cart, 
      $cart = $_SESSION['cart'];
      $IDOoS = $row9['IDOoS'];
      unset($cart[$IDOoS]);
      
      }
      

        It does so by the use of [man]unset/man. As NogDog has noted, you could use array_filter().

          function my_func($val) {
              return ($val != "foo");
          }
          
          $array = array("foobar", "bar", "hello", "foo", "world!");
          $array = array_filter($array, 'my_func');
          
          // $array is now: array("foobar", "bar", "hello", "world!")

            so....

            This is my array:
            Array ( [0] => Array ( [ID] => 29 [ItemNumber] => 1 [Name] => GL1020 [Size] => Plus size [Color] => Black ) [1] => Array ( [ID] => 29 [ItemNumber] => 2 [Name] => GL1020 [Size] => Plus size [Color] => Black ) )

            $cart = array_filter($cart, 'my_func');
            $_SESSION['cart'] = $cart;
            

            How would I remove "my_func" this would be on of these from ID to Color.... how would I do this?

              How would I remove "my_func" this would be on of these from ID to Color.... how would I do this?

              In bradgrafelman's example, my_func is the callback function passed to array_filter. array_filter loops over the array, calling my_func with the current array element as the argument. If my_func returns true, the current array element is retained.

              So, the idea is to determine what is the condition by which you would want to keep or remove an element, then implement this in the callback function.

                Okay I've changed my array now for other reasons...

                Array1 = 12, 12, 5, 6, 55, 12
                Array2 = 12, 6, 12

                I want to remove all items in array2 which are in array1. Not 12 is there 3 times in Array1 and twice in array2. So I want to remove only 2 from array1?

                  Consider this example:

                  <?php
                  $array1 = array(12, 12, 5, 6, 55, 12);
                  $array2 = array(12, 6, 12);
                  
                  foreach ($array1 as $key => $value) {
                      if (($found_key = array_search($value, $array2)) !== false) {
                          unset($array1[$key]);
                          unset($array2[$found_key]);
                      }
                  }
                  $array1 = array_values($array1);
                  
                  print_r($array1);
                  ?>
                    $array1 = array_diff($array1, $array2);
                    

                      array_diff() does not work here since it treats the arrays like sets, and for sets there are no duplicates.

                        laserlight;10881188 wrote:

                        array_diff() does not work here since it treats the arrays like sets, and for sets there are no duplicates.

                        Ugh. I guess he has his reasons. :rolleyes:

                          laserlight;10881161 wrote:

                          Consider this example:

                          <?php
                          $array1 = array(12, 12, 5, 6, 55, 12);
                          $array2 = array(12, 6, 12);
                          
                          foreach ($array1 as $key => $value) {
                              if (($found_key = array_search($value, $array2)) !== false) {
                                  unset($array1[$key]);
                                  unset($array2[$found_key]);
                              }
                          }
                          $array1 = array_values($array1);
                          
                          print_r($array1);
                          ?>

                          This works very very well, can you comment it so I can better understand each line?

                            Write a Reply...