temporarily apply a function to an associative array's values

Discussion in 'PHP' started by soulscratch, May 8, 2007.

  1. #1
    Example:

    $bulls = array(
    'Michael Jordan',
    'Scottie Pippen',
    'Dennis Rodman',
    'Tony Kukoc',
    'Luc Longley'
    );

    Now, I have a form that asks the user to enter the name of a player, to see if that player (or string) is in this team (or array).

    Let's say I clicked submit, and $_POST['player-name'] is equal to 'michael jordan'.

    // the submit input has a name attribute equal to submitted
    if (isset($_POST['submitted'])) {
    if(isset($_POST['player-name'] && !empty($_POST['player-name']) {
    $player = $_POST['player-name'];
    if(in_array($player, $bulls)) {
    echo $player . ' ' . 'is in the Bulls.';
    } else {
    echo $player . ' ' . 'is <strong>not</strong> in the Bulls.';
    }
    }
    }

    (i may have screwed up on the end brackets, sorry for that).

    This will check if the value of the $player variable is inside the $bulls array, but how can I make it so that it converts the $player variable to lowercase and every value in $bulls to lowercase, so that capitalized letters wont matter?

    Thanks in advance.
     
    soulscratch, May 8, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $bulls = array_map('strtolower', $bulls);
    
    PHP:
     
    nico_swd, May 9, 2007 IP