amirelgohary1990 Posted December 1, 2023 Share Posted December 1, 2023 Hello, I am trying to unset from array using array_search, it's working, except the first array value "a+" is not working $bloodType_list = ['a+','a-','b+','b-','o+','o-','ab+','ab-']; if($key = array_search('a+',$bloodType_list)){ unset($bloodType_list[$key]); } foreach($bloodType_list as $bloodType_lists){ echo $bloodType_lists."<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/317500-unset-from-array-using-array_search/ Share on other sites More sharing options...
mac_gyver Posted December 1, 2023 Share Posted December 1, 2023 the key/index for the 'a+' entry is 0. this is a boolean false value, so the if() logic branch is skipped. you need to perform an exact match for a non-false value. e.g. !== false Quote Link to comment https://forums.phpfreaks.com/topic/317500-unset-from-array-using-array_search/#findComment-1613257 Share on other sites More sharing options...
amirelgohary1990 Posted December 1, 2023 Author Share Posted December 1, 2023 2 hours ago, mac_gyver said: the key/index for the 'a+' entry is 0. this is a boolean false value, so the if() logic branch is skipped. you need to perform an exact match for a non-false value. e.g. !== false You mean like below code ? $bloodType_list = ['a+','a-','b+','b-','o+','o-','ab+','ab-']; if($key = array_search('a+',$bloodType_list)) !== false { unset($bloodType_list[$key]); } foreach($bloodType_list as $bloodType_lists){ echo $bloodType_lists."<br>"; } I tried that not working too Quote Link to comment https://forums.phpfreaks.com/topic/317500-unset-from-array-using-array_search/#findComment-1613258 Share on other sites More sharing options...
Barand Posted December 1, 2023 Share Posted December 1, 2023 Your "( .. )" are in the wrong places. Quote Link to comment https://forums.phpfreaks.com/topic/317500-unset-from-array-using-array_search/#findComment-1613260 Share on other sites More sharing options...
maxxd Posted December 2, 2023 Share Posted December 2, 2023 Don't try to assign and compare on the same line; it makes things complicated. Assign the array_search result to $key on it's own line, absolutely compare $key to false. If $key is not false, unset the value at index $key. Quote Link to comment https://forums.phpfreaks.com/topic/317500-unset-from-array-using-array_search/#findComment-1613262 Share on other sites More sharing options...
amirelgohary1990 Posted December 2, 2023 Author Share Posted December 2, 2023 6 hours ago, maxxd said: Don't try to assign and compare on the same line; it makes things complicated. Assign the array_search result to $key on it's own line, absolutely compare $key to false. If $key is not false, unset the value at index $key. I tried this also, but same problem $bloodType_list = ['a+','a-','b+','b-','o+','o-','ab+','ab-']; if($key = array_search('a+',$bloodType_list)){ if($key !== false){ unset($bloodType_list[$key]); } } foreach($bloodType_list as $bloodType_lists){ echo $bloodType_lists."<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/317500-unset-from-array-using-array_search/#findComment-1613267 Share on other sites More sharing options...
amirelgohary1990 Posted December 2, 2023 Author Share Posted December 2, 2023 10 hours ago, Barand said: Your "( .. )" are in the wrong places. It was inside the () just I wrote here by wrong Quote Link to comment https://forums.phpfreaks.com/topic/317500-unset-from-array-using-array_search/#findComment-1613268 Share on other sites More sharing options...
Solution maxxd Posted December 2, 2023 Solution Share Posted December 2, 2023 You're still setting the $key variable inside a conditional. $key = array_search('a+', $bloodType_list); Don't wrap it in a conditional, just make the assignment a statement. Then check the value of the variable against false (which you are currently doing). Quote Link to comment https://forums.phpfreaks.com/topic/317500-unset-from-array-using-array_search/#findComment-1613270 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.