Jump to content

Duplicate key in array


Cyto

Recommended Posts

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

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'));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.