Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    case insensitive in array

    PHP Code:
    $letter = array
        (
            
    1    =>    A,
            
    2    =>    B,
            
    3    =>    C,
            
    4    =>    D,
        
        );

    echo  
    array_search("a"$letter); 
    it cannot find lowercase a why ?

  • #2
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    This was in the user comments at http://us2.php.net/array_search

    PHP Code:
    <?php
        
    function array_isearch($str$array) {
            foreach(
    $array as $k => $v) {
                if(
    strcasecmp($str$v) == 0) return $k;
            }
            return 
    false;
        }
    ?>
    What I usually do is:
    PHP Code:
    foreach($array as $x) {
        if(
    strcmp(strtoupper($x),strtoupper($key)) {
            return 
    $x;
        }

    Which is about the same thing except the first example takes care of case with strcasecmp while I take care of it with strtoupper. Theirs is probably faster.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •