从数组中删除重复值,忽略最后两个字符

I am working on a small php script, currently i have an array like this

[0] yassine#m, [1] yassine#f, [2] Dolmi#m , [3] yassine#l

I want PHP to check if there is a duplicated element (yassine in this case) and return something like this.

[0] yassine , [1] Dolmi#m

array_unique won't work. And i really don't have any clue how to solve this. If looked for a solution on the internet but doesnt seem to find it. Anyone can help Please ?

I think this may work for you.

First sort array by value, then use combination of substr(), strpos() and array_push() to create new array according to your need then remove duplicate value using array_unique()

<?php
$oldarray = array("suman#1","suman#2","suman#3","sujan#1","suresh#2",""); 
// first sort array by value so matching value comes together
asort($oldarray);
$newarray = array();
$count = count($oldarray);
for($i=0; $i < $count-1; $i++){
 $a = $oldarray[$i];
 $b = $oldarray[$i+1];
    if($i == 0)
        $c = "";
    else 
        $c = $oldarray[$i-1];
    if(substr($a,0,strpos($a,"#")) == substr($b,0,strpos($b,"#")) || substr($a,0,strpos($a,"#")) == substr($c,0,strpos($c,"#")) ){
     array_push($newarray,substr($a,0,strpos($a,"#")));
    }
     else
        array_push($newarray,$a);
} 
print_r($oldarray);
// now remove duplicate value from new array
$newarray = array_unique($newarray);
print_r($newarray);

?>

Check following solution

http://ideone.com/fork/kJlLbs

<?php

function generateUniqueList ($arr){
  $ret = array();

  foreach ($arr as $value) {
      $key = explode("#", $value)[0];
      if (array_key_exists($key, $ret)) {
        $ret[$key] = $key;
      }
      else {
        $ret[$key] = $value;
      }
  }

  return array_values($ret);
}
$arr  = array("yassine#m","yassine#f","Dolmi#m", "yassine#l");
$list = generateUniqueList ($arr);
print_r($list);