Cyto Posted April 24, 2012 Share Posted April 24, 2012 Hi, I have a function that returns the value of a array if it matches $string, but it only returns Test 1 instead of returning Test 1, Test 2. Can you someone see what I'm doing wrong? And it has to return N if nothing found. Code: function fes($string){ $fes['Test 1'] = '104'; $fes['Test 2'] = '104'; return in_array($string, $fes) ? array_search($string, $fes) : 'N'; } echo fes('104'); Thx, Cyto Link to comment https://forums.phpfreaks.com/topic/261512-duplicate-key-in-array/ Share on other sites More sharing options...
MadTechie Posted April 24, 2012 Share Posted April 24, 2012 Okay first off the array_search will find the key but it will ONLY return the first one found, Try this function fes($string) { $fes['Test 1'] = '104'; $fes['Test 2'] = '104'; $return = array(); foreach($fes as $key => $f){ if($f==$string) $return[] = $key; } return (!empty($return))?$return:'N'; } echo implode(",",fes('104')); Link to comment https://forums.phpfreaks.com/topic/261512-duplicate-key-in-array/#findComment-1340026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.