rmelino Posted January 3, 2010 Share Posted January 3, 2010 Hello, I've been struggling to figure this out and can't seem to make it work. I'm using the array_search function. The problem occurs when I am searching for a value that appears twice in the array. In the example below, you'll see that '1-2-3' is the value for both 'Dog Brown' and 'Dog Black'. When I post, I am only able to echo out 1-2-3 when the posted values are $animal = 'Dog' and $color = 'Brown'. If i post $animal = 'Dog' and $color = 'Black' the it echoes out nothing. I am assuming this is because '1-2-3' appears twice in the array and 'Dog Brown' is the first one in the array list? Any help is greatly appreciated! Thanks in advance... if (isset($_POST['calculate']) || isset($_POST['calculate_x'])) { $animal = ($_POST['animal']); $color = ($_POST['color']); } $search = $animal.''.$color; $array = array( '1-2-3' => 'Dog Brown', '1-2-3' => 'Dog Black', '4-5-6' => 'Cat Brown', '7-8-9' => 'Bird Black' }; $result = array_search($search,$array); echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/ Share on other sites More sharing options...
MadTechie Posted January 3, 2010 Share Posted January 3, 2010 You can't use the same key for different values '1-2-3' => 'Dog Black' is replacing '1-2-3' => 'Dog Brown', so 'Dog Brown' doesn't exist, Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/#findComment-987808 Share on other sites More sharing options...
rmelino Posted January 3, 2010 Author Share Posted January 3, 2010 Hi, Thanks for the reply. So how do I get around this then? I need to pull '1-2-3' for both Dog Black and Dog Brown... Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/#findComment-987809 Share on other sites More sharing options...
salathe Posted January 3, 2010 Share Posted January 3, 2010 You could swap around the array keys and values, like: $array = array( 'Dog Brown' => '1-2-3', 'Dog Black' => '1-2-3', 'Cat Brown' => '4-5-6', 'Bird Black' => '7-8-9' }; $result = 'No match'; if (isset($result[$search])) { $result = $result[$search]; } echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/#findComment-987817 Share on other sites More sharing options...
rmelino Posted January 4, 2010 Author Share Posted January 4, 2010 Thanks Salathe. This still isn't working for me because when I swap around the array keys and values my array_search function no longer works. Perhaps I'm not fully understanding what you meant with this part of your code: $result = 'No match'; if (isset($result[$search])) { $result = $result[$search]; } echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/#findComment-987829 Share on other sites More sharing options...
salathe Posted January 4, 2010 Share Posted January 4, 2010 The intention was to show that you don't use array_search with my code snippet. The if/isset block replaces your use of array_search. Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/#findComment-987837 Share on other sites More sharing options...
MadTechie Posted January 4, 2010 Share Posted January 4, 2010 slight typo in salathe code, it should be $result = 'No match'; if (isset($array[$search])) { $result = $array[$search]; } Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/#findComment-987845 Share on other sites More sharing options...
salathe Posted January 4, 2010 Share Posted January 4, 2010 Cheers MadTechie, I am a little cross-eyed. Apologies for the typo. :-) Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/#findComment-987851 Share on other sites More sharing options...
MadTechie Posted January 4, 2010 Share Posted January 4, 2010 On first glance I missed it too, (PS: I know it was a typo, as your coding is normally a high standard) Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/#findComment-987859 Share on other sites More sharing options...
rmelino Posted January 4, 2010 Author Share Posted January 4, 2010 Ahh, thanks everyone. I was having trouble figuring out how that would work without any mention of $array! Thanks again... Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/#findComment-987869 Share on other sites More sharing options...
MadTechie Posted January 4, 2010 Share Posted January 4, 2010 I have marked this as solved, (you could do this itself by clicking "Mark Solved") or "Mark Unsolved" to unsolved it. Quote Link to comment https://forums.phpfreaks.com/topic/187056-problem-with-array_search/#findComment-987886 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.