Welcome Guest, Not a member yet? Register   Sign In
Search through cart multi-dimensional array
#1

[eluser]digitaleye[/eluser]
Hi
I need a little help with a function.

I am trying to search through a multi dimensional array (the Codeigniter cart) to find any items that have an id of a certain value.
Each item in the cart has the basic values (id, price etc). While displaying all items (in a foreach loop) i want to see if that item is already in the cart and return TRUE/FALSE.

The code i have for the function is :
Code:
function isInCart($array, $key, $value)
    {
        # check if an item is in the cart
        foreach($array as $item) {
            if($item[$key] == $value) {
                return TRUE;
            } else {
                return FALSE;
            }
        }
    }

And i am passing in
Code:
$this->cart->contents()
as the array
'id' as the key to search (in this case the id
and
Code:
$cp['id']
which is the item id generated from the foreach loop in the view.

However, it only seems to work for the first value in the array.

Any help would be gratefully appreciated.

Thanks
#2

[eluser]digitaleye[/eluser]
I have actually managed to solve this now :

The multi-dimensional array search should have been

Code:
function isInCart($array, $key, $value)
    {
        # check if an item is in the cart
        foreach($array as $k => $v) {
            if($v[$key] == $value) {
                return TRUE;
            }
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB