PHP: Case-insensitive “array_diff”

前端 未结 2 1136
北海茫月
北海茫月 2021-02-07 07:35

I have following two arrays and the code to find array_diff:

$obs_ws = array(\"you\", \"your\", \"may\", \"me\", \"my\", \"etc\");
$all_ws = array(\"LOVE\", \"Wo         


        
相关标签:
2条回答
  • 2021-02-07 08:06

    Try to pass strcasecmp as third parameter to array_udiff function:

    <?php
    $obs_ws = array("you", "your", "may", "me", "my", "etc");
    $all_ws = array("LOVE", "World", "Your", "my", "etc", "CoDe");
    
    $final_ws = array_udiff($all_ws, $obs_ws, 'strcasecmp');
    
    print_r($final_ws);
    

    Output:

    Array
    (
        [0] => LOVE
        [1] => World
        [5] => CoDe
    )
    
    0 讨论(0)
  • 2021-02-07 08:16

    You were on the right track. This is my suggestion:

    function array_casecmp($arr1,$arr2){
        return array_udiff($arr1,$arr2,'strcasecmp');
    }
    
    
    $obs_ws = array("you", "your", "may", "me", "my", "etc");
    $all_ws = array("LOVE", "World", "Your", "my", "etc", "CoDe");
    var_dump( array_casecmp($all_ws,$obs_ws) );
    
    0 讨论(0)
提交回复
热议问题