FewFlyBy FewFlyBy - 1 month ago 6
PHP Question

How to get value of a given key if found in an array?

I'm trying to get the value of a given key if found using the

array_key_exists
function.

$var = 35;

if(array_key_exists($var, $_SESSION['cart_items']))
{
//give the value of found key
}
else
{
$_SESSION['cart_items'][$var] = $anothervar;
}


For now, I only have this:

Array
(
[35] => 3
[41] => 6
)


So what I wanted to get is the value
3
. Is there a simple way to do this?

Answer

Don't quite understand, but I assume your $_SESSION['cart_items'] is:

Array
(
    [35] => 3
    [41] => 6
)

Then, to get value 3, all you need is:

$_SESSION['cart_items'][$var]