I am using php to extract category names from a series of woocommerce (wordpress) cart items like so:
<?php $stack = array();
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$category = $_product->get_categories();
array_push($stack, $category);
}
If there are two products in the cart, each with a different category, the output of print_r($stack)
might be: Array ( [0] => Thanksgiving [1] => Poultry )
Regardless of products, I need to run conditional statements like so:
if (in_array( 'T-Shirts', $stack[1] )) echo "True"; else echo "False";
The above conditional is only looking at the 2nd item in the array, not all. How do I search through all items in array?
$categories[1] is an array where each element is a single category. in_array() checks to see if the requested value is ANYWHERE in the array.
Test it:
yields:
Update
Integrating with your code:
If you don't want a category to appear more than once in the $stack array: