tec-4 Posted November 12, 2011 Share Posted November 12, 2011 Hello, Currently have an array like looks like the following when using print_r($session->items): Array ( [F1225] => 1 [EDS11147317] => 1 [1156D6RE001] => 1 [i111ADE47946] => 1 [s679AQ339] => 1 [s67914599] => 1 [CH111337631254] => 1 [s6787903647] => 1 [A11144O189] => 1 [F11144520] => 1 [121584Q12] => 1 [i11144661ADE] => 1 [s678829NB] => 1 ) I am trying to check if an item is in the array and if so, display a different result but currently every item says it is in the array when they are not (unless my logic is off...from the items I am looking at some should be included and some should not..but the below code is showing every item as having the same result). Example: foreach ($items as $item) { if (in_array($item->ItemID, $session->items)) { //$session->items is the array output above echo "In Array"; } else { echo "NOT in Array"; } } Currently everything say "In Array" when some should not. Am I using the in_array incorrectly? Thanks Everyone. Quote Link to comment https://forums.phpfreaks.com/topic/251026-checking-if-an-item-is-in-an-array-using-in_array-but-not-working-as-expected/ Share on other sites More sharing options...
xyph Posted November 12, 2011 Share Posted November 12, 2011 All your you array values are keys. You probably want to use array_key_exists. Without knowing the value of $item->ItemID it's impossible to help further. Quote Link to comment https://forums.phpfreaks.com/topic/251026-checking-if-an-item-is-in-an-array-using-in_array-but-not-working-as-expected/#findComment-1287682 Share on other sites More sharing options...
tec-4 Posted November 12, 2011 Author Share Posted November 12, 2011 Well that was a quick fix...and here I thought I was going crazy Thanks for looking it over xyph! Quote Link to comment https://forums.phpfreaks.com/topic/251026-checking-if-an-item-is-in-an-array-using-in_array-but-not-working-as-expected/#findComment-1287684 Share on other sites More sharing options...
xyph Posted November 12, 2011 Share Posted November 12, 2011 Wait! Incorrect terminology error Don't wanna confuse you Change the first line of my post to Your array data is stored in the key, not as a value. Quote Link to comment https://forums.phpfreaks.com/topic/251026-checking-if-an-item-is-in-an-array-using-in_array-but-not-working-as-expected/#findComment-1287693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.