2016-03-25 15 views
1

Hey Leute durch Werte Multidimensional Array Artikel löschen habe ich dieses Array:Im Artikel

Array 
(
    [qty] => 1 
    [id] => 2 
    [name] => sallate me gjera plot 
    [price] => 8 
    [category_id] => 25 
    [dish_name] => sallate me gjera plot 
    [dish_id] => 2 
    [dish_category_id] => 25 
    [dish_qty] => 1 
    [dish_price] => 8 
) 
Array 
(
    [qty] => 1 
    [id] => 1 
    [name] => sallate cezar 
    [price] => 12 
    [category_id] => 25 
    [dish_name] => sallate cezar 
    [dish_id] => 1 
    [dish_category_id] => 25 
    [dish_qty] => 1 
    [dish_price] => 12 
) 

Und was ich versuche, das Element von gericht_id unscharf zu schalten zu tun ist. Dies ist die Art, wie ich tun beabsichtige, dass:

if(isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"])>0) 
     { 
      foreach ($_SESSION["cart_products"] as $key =>$cart_itm) 
      { 
       if($cart_itm["dish_id"]==$removable_id) 
       { 
        unset($cart_itm[$key]); 
       } 
      } 
     } 

Kann mir jemand sagen, was ich tue worng..thanks: D

+1

Änderung 'unset ($ cart_itm [$ key ]); 'zu' unset ($ _ SESSION ["cart_products"] [$ key]) 'und überprüfen –

+0

Das war es dank viel: D –

+0

sorry @Anant zuerst d ay here..Ich bin daran gewöhnt:/ –

Antwort

1

Eigentlich müssen Sie unset Daten von tatsächlichen Array, die $_SESSION["cart_products"] nicht $cart_itm ist.

So unset($cart_itm[$key]); ändern unset($_SESSION["cart_products"][$key])

1

Sie müssen aus der $_SESSION["cart_products"] Variable

if(isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"])>0) 
    { 
     foreach ($_SESSION["cart_products"] as $key =>$cart_itm) 
     { 
      if($cart_itm["dish_id"]==$removable_id) 
      { 
       unset($_SESSION["cart_products"][$key]); 
      } 
     } 
    } 
unscharf zu schalten
1

Als Alternative Sie array_filter() mit einer anonimous Funktion verwenden können:

$removable_id = 1; 

$_SESSION["cart_products"] = array_filter 
( 
    $_SESSION["cart_products"], 
    function($row) use($removable_id) 
    { 
     return $row['dish_id'] != $removable_id; 
    } 
); 

print_r($_SESSION["cart_products"]); 

druckt:

Array 
(
    [0] => Array 
     (
      [qty] => 1 
      [id] => 2 
      [name] => sallate me gjera plot 
      [price] => 8 
      [category_id] => 25 
      [dish_name] => sallate me gjera plot 
      [dish_id] => 2 
      [dish_category_id] => 25 
      [dish_qty] => 1 
      [dish_price] => 8 
     ) 

) 
+0

danke für alle Antworten ... viel gelernt heute: D –

Verwandte Themen