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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.