views:

88

answers:

2

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", "World", "Your", "my", "etc", "CoDe");

$final_ws = array_diff($all_ws, $obs_ws);

Above code giving output array as:

$final_ws = array("LOVE", "World", "Your", "CoDe");

But I want it as:

$final_ws = array("LOVE", "World", "CoDe");

Note "Your" is not removed, it may be due to "Y" is in caps in second array. I want to exclude "Your" also, so is there any case-insensitive version of array_diff in PHP.

I tried array_udiff but I am not getting exactly how to use this in my problem

Thanks

+8  A: 

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
)
Ivan Nevostruev
I believe your missing a comma. ;)
MitMaro
@MitMaro: Thanks!
Ivan Nevostruev
I tried it like `array_udiff($all_ws, $obs_ws, 'strcasecmp');` but its not working :(
Prashant
@Prashant: Sorry, there was a small mistype in the name of the variable. Try it now. I've tested it.
Ivan Nevostruev
But its not working, its not excluding "Your" from the resulting array ??
Prashant
I've added full source and output, which works. Please test it as is.
Ivan Nevostruev
Can't figure out what was the problem, but its working now. :) Thanks
Prashant
+1  A: 

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) );
Jage
Why not just pass function name `'strcasecmp'` as third parameter?
Ivan Nevostruev
You're right, I should.
Jage