Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP ignore case sensitivity when comparing array values

Tags:

php

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.

like image 934
dan.codes Avatar asked Dec 28 '22 22:12

dan.codes


1 Answers

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 ( )

like image 88
Darbio Avatar answered Jan 12 '23 21:01

Darbio