1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Opposite of array_search function?

Discussion in 'PHP' started by norfstar, Sep 16, 2005.

  1. #1
    array_search (http://uk.php.net/manual/en/function.array-search.php) searches an array for a value an returns the corresponding key. What I need to do is search an array for a key and return the corresponding value.

    Now I know the first thing you may think is that I'm really silly and forgot it's just $foo[bar]. However, the key itself will be variable, and I can't put $foo[$keynumber] as anyone who is familiar with PHP will know that is incorrect.

    I'm sure it's simple. Can anyone give me a pointer? Thanks :D.
    SEMrush
     
    norfstar, Sep 16, 2005 IP
    SEMrush
  2. dct

    dct Finder of cool gadgets

    Messages:
    3,134
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #2
    I'm not an expert at PHP but you can do
    
    $keyname = 'whateveristhatkeyname';
    echo $foo[$keyname];
    
    PHP:
     
    dct, Sep 16, 2005 IP
  3. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Unfortunately you can't :(.

    I'm thinking perhaps there is a way to switch the keys and values in an array, then I could use array_search. If anyone knows how this can be done that'd be appreciated :).
     
    norfstar, Sep 16, 2005 IP
  4. dct

    dct Finder of cool gadgets

    Messages:
    3,134
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #4
    I don't know if its a version thing but I'm using that method successfully now, I've tested it to make doubly sure and it works fine :confused:

    Can you post some sample code that isn't working?
     
    dct, Sep 16, 2005 IP
    Willy likes this.
  5. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #5
    :eek: That's exactly how it's done, in fact the whole point of arrays and hashtables is that you can lookup the value using the key, and of course it can be a variable :confused: dct's example will work perfectly.
     
    Willy, Sep 16, 2005 IP
    norfstar and dct like this.
  6. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #6
    My mistake :eek:. I'm sure I'd tried that a while back on a different script and it didn't work - maybe like dct said it's a version thing (or maybe I'm just an idiot :eek:).

    Thanks!
     
    norfstar, Sep 16, 2005 IP
    Willy likes this.
  7. sarahk

    sarahk iTamer Staff

    Messages:
    25,780
    Likes Received:
    3,652
    Best Answers:
    103
    Trophy Points:
    665
    #7
    try

    $keyname = 'whateveristhatkeyname';
    if (isset($foo[$keyname])) echo "I've already added {$keyname}";
    Code (markup):
     
    sarahk, Sep 17, 2005 IP