4

I have to modify some code in a application I am working on that is using the array_diff($array1,$array2) method. The problem I am having is it is case sensitive and I need to have it return the correct value if the array values match even if the case is different. I don't want to change the case to lowercase because I need the value returned to keep its case. I'm a little confused as the best method to do this.

3 Answers 3

10

You need: array_udiff and strcasecmp

$result = array_udiff($arr1, $arr2, 'strcasecmp');

E.g.

<?php
$arr1 = array("string","string","string");
$arr2 = array("String","string","sTRING");

$result = array_udiff($arr1, $arr2, 'strcasecmp'); 
print_r($result);
?>

$result should echo array ( )

Sign up to request clarification or add additional context in comments.

Comments

0

Use

strcasecmp — Binary safe case-insensitive string comparison

<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
    echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>

Refer this link for more information

3 Comments

This is comparing arrays, not strings.
see my example above for array comparing, using @pavun_cool's strcasecmp and array_udiff
not sure that works for my situation, I am comparing two arrays whatever values are not in both get returned and then those are used
0

Serializing can help, so you can use strcasecmp on the resulting strings:

<?php

$arr1 = array("string","string");
$arr2 = array("String","sTRING");

$equal = (strcasecmp(serialize($arr1), serialize($arr2)) === 0);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.