filoaman Posted September 22, 2013 Share Posted September 22, 2013 Hi I'm trying to use array_search in a loop without success. i will appreciate any feed back. here is mu code: $itemsToSearch=array("item1","item2","item3") // An array with the tems I like to search $ArrayToSearchIn=array("item1","item2","item3","item5","item5","item6") // Here is that array I try to execute the search $i=0; while ($i<3){ $searchTerm=$itemsToSearch[$i]; $key=array_search($searchTerm, $ArrayToSearchIn); print $key."<br>"; $i++; } Now what i get as result is something like this: <br> (an empty line with line brake) <br> (an empty line with line brake) 2 (the key number ONLY for the third item "item3" or "$itemsToSearch[2]") I was expecting something like this: 0 (the key number for the first item "item1" or "$itemsToSearch[0]") 1 (the key number for the second item "item2" or "$itemsToSearch[1]") 2 (the key number for the first third "item3" or "$itemsToSearch[2]") What is wrong with my code? Any ideas? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/282365-array_search-in-loop-without-succes/ Share on other sites More sharing options...
vinny42 Posted September 22, 2013 Share Posted September 22, 2013 The code you posted contains two syntax errors but is otherwise fine. So, what does your real code look like? Quote Link to comment https://forums.phpfreaks.com/topic/282365-array_search-in-loop-without-succes/#findComment-1450711 Share on other sites More sharing options...
filoaman Posted September 22, 2013 Author Share Posted September 22, 2013 @vinny42 Assume that the syntax errors are i forgot to add the ";" symbol at the end of my original array declaration $itemsToSearch=array("item1","item2","item3"); // An array with the tems I like to search $ArrayToSearchIn=array("item1","item2","item3","item5","item5","item6"); // Here is that array I try to execute the search $i=0; while ($i<3){ $searchTerm=$itemsToSearch[$i]; $key=array_search($searchTerm, $ArrayToSearchIn); print $key."<br>"; $i++; } Are there any other syntax errors? The original code is almost identical. I only change the names of the variables in order to make things more clear for the users to help. You mention that, apart from the syntax errors, you get the result i want to get using this code. Is that true? Quote Link to comment https://forums.phpfreaks.com/topic/282365-array_search-in-loop-without-succes/#findComment-1450714 Share on other sites More sharing options...
Barand Posted September 22, 2013 Share Posted September 22, 2013 Have you considered array_intersect()? $itemsToSearch=array("item1","item2","item3"); $ArrayToSearchIn=array("item1","item5","item2","item3","item5","item6"); $res = array_intersect($ArrayToSearchIn, $itemsToSearch); echo '<pre>',print_r($res, true),'</pre>'; /**************************** OUTPUT Array ( [0] => item1 [2] => item2 [3] => item3 ) *******************************/ Quote Link to comment https://forums.phpfreaks.com/topic/282365-array_search-in-loop-without-succes/#findComment-1450715 Share on other sites More sharing options...
vinny42 Posted September 22, 2013 Share Posted September 22, 2013 You mention that, apart from the syntax errors, you get the result i want to get using this code. Is that true? Run the code yourself, it prints 0 1 2... If the only change is the names of the vars then the names of the varsin your real code are probably wrong. Quote Link to comment https://forums.phpfreaks.com/topic/282365-array_search-in-loop-without-succes/#findComment-1450716 Share on other sites More sharing options...
filoaman Posted September 22, 2013 Author Share Posted September 22, 2013 OK - I just realize what happen! As vinny42 mention the code works. The problem was that i originally create the "$itemsToSearch" array from data posted from a form. So at the end of every "item" there was a brake line (\r). Although I echo the "items" on screen, it was impossible to see the line brake! So although the array_search works perfect, can't find the "item\r" on "$ArrayToSearchIn" array. Since the last "item" was entered in the "$itemsToSearch" array from the post form without (\r) brake, that's why i was able to get the $key only for the last "item"! I clear the entries using this little line of code: $searchTerm=str_replace("\r", '', $searchTerm); and now everythig is fine! Thank you for your help and you feedback! Quote Link to comment https://forums.phpfreaks.com/topic/282365-array_search-in-loop-without-succes/#findComment-1450741 Share on other sites More sharing options...
vinny42 Posted September 23, 2013 Share Posted September 23, 2013 Good to hear. However I would advise you to look at array_intersect() too, if that does what you need it is certainly much more efficient than looping with array_search(). Quote Link to comment https://forums.phpfreaks.com/topic/282365-array_search-in-loop-without-succes/#findComment-1450759 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.