-2
$tags_array = explode(',', $tagss);

this gives result like.

Array ( [0] => Katha [1] => pooja [2] => singer [3] => katha )

when i use array_unique not working gives same result.

//print_r($tags_array);
print_r(array_unique($tags_array));

Array ( [0] => Katha [1] => pooja [2] => singer [3] => katha )

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

2 Answers2

0

You have no duplicate values in your array. From the manual page:

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2 i.e. when the string representation is the same, the first element will be used.

However, there is a solution in the user-contributed notes on that page:

function array_iunique($array) { 
    $lowered = array_map('strtolower', $array); 
    return array_intersect_key($array, array_unique($lowered)); 
} 
alanlittle
  • 460
  • 2
  • 12
0

Because your input array is collection of non-numeric strings, you can loop over the array and populate a new array with temporary, lowercase keys and use the null coalescing assignment operator to keep only the first occurring (case-insensitive) value.

Code: (Demo)

$array = explode(',', 'Katha,pooja,singer,katha');

$result = [];
foreach ($array as $value) {
    $result[strtolower($value)] ??= $value;
}
var_export(array_values($result));
// ['Katha', 'pooja', 'singer']

By replacing the ??= with =, you will effectively retain the latest occurring value per case-insensitive key. (Demo)

// ['katha', 'pooja', 'singer']
mickmackusa
  • 43,625
  • 12
  • 83
  • 136