add('pizza'); $set->add('hamburger'); $set->add('taco'); $set->add('pizza'); // This will not be added to the set because it already exists print_r($set); ?>
add('apples'); $cart->add('bananas'); $cart->add('bread'); $cart->add('milk'); $cart->add('cookies'); $cart->add('apples'); // This will not be added to the cart because it already exists $cart->add('chips'); $totalItems = $cart->count(); echo "Your shopping cart contains $totalItems items: "; foreach ($cart as $item) { echo "$item, "; } ?>In this example, a shopping cart is represented by a set, and items are added to the cart using the set insert function. Again, any duplicate items are not added to the set. Finally, the number of items in the cart is displayed, along with a list of all the unique items in the cart. The Ds\Set package library is used to create the shopping cart set.