Possible Duplicate:
How check any value of array exist in another array php?
I am creating a shopping site. To simplify, I have 2 arrays, one holds all my items and the other holds all the items that are added on the cart :
$Items
Array
(
[0] => stdClass Object
(
[id] => 1
[title] => Verity soap caddy
[price] => 6.00
)
[1] => stdClass Object
(
[id] => 2
[title] => Kier 30cm towel rail
[price] => 14.00
)
//a lot more
)
$cartItems
Array
(
[0] => Array
(
[rowid] => c4ca4238a0b923820dcc509a6f75849b
[id] => 1
[qty] => 1
[price] => 6.00
[name] => Verity soap caddy
[subtotal] => 6
)
)
I would like to loop through the $cartItems and add a class (identify) if an item is also in the cart. This is how I tried to do it
foreach($items as $items){
if($cartItems[$items->id]['id']){
echo '<h1 class="inCart">'. $item->title . '</h1>' //...
}else{
echo '<h1>'. $item->title . '</h1>' //...
}
}
The above code does not work – even though $cartItems[0]['id'] would return what I need. My thinking is, whilst looping through $items, check if the similar id exists in the $cartItems array. I also tried to add $cartItems[$i]['id'] and increment $i within the loop, and that did not work.
Of course the html output that I wanted to get is (simplified)
<h1 class="IsOnCart"> Item Title</h1>
<h1> Item Title</h1>
<h1 class="IsOnCart"> Item Title</h1>
<h1> Item Title</h1>
Is there a way to implement this?
Thanks
1 Answer