0

I am want to remove duplicated value from array so i am doing this

$input = array("APPL", "berry", "apple", "berry");
$result = array_unique($input);
print_r($result);

So it will output:

APPLE,apple,berry

I want to get result like output apple only once despite of lowercase and uppercase, how can i do this? is there any function in PHP like "array_unique" which i have to use instead, in situation like this? (without making all characters upper or lowercase)

8
  • 6
    possible duplicate of case-insensitive array_unique Commented Aug 14, 2015 at 9:52
  • [case-insensitive array_unique][1] [1]: stackoverflow.com/questions/2276349/… Commented Aug 14, 2015 at 9:58
  • 1
    First of all, I assume you meant to write $input = array("APPLE", "berry", "apple", "berry");. Secondly, if you are looking for a result of ['APPLE', 'apple', 'berry'] you are doing a case-sensitive filter, not case-insensitive. In which case, just use array_unique() Commented Aug 14, 2015 at 10:03
  • @Darragh how do i make it case-insensitive? i don't want to change all of them in upper or lowercase. Commented Aug 14, 2015 at 10:06
  • I have post my answer check it and let me know Commented Aug 14, 2015 at 10:09

3 Answers 3

2

Try this..

<?php
$input = array("APPLE", "berry", "apple", "berry");

$data = array_intersect_key($input, array_unique(array_map('strtolower', $input)));

print_r($data);
?>

https://eval.in/416659

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

Comments

0

An array is case sensitive. No function to ignore it while printing, but you may change the case before. Try this :

$input = array("APPLE", "berry", "apple", "berry");
$lower_input = array_map('strtolower', $input);
$result2 = array_unique($lower_input);
print_r($result2);

Comments

-1

Use:

function array_iunique($array) {
        return array_intersect_key(
            $array,
            array_unique(array_map("StrToLower",$array))
        );
    }

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.