Say the data you have is in a file, you'll need to use associative arrays to get the job done!
$arr = array();
// Read the data an get it into array $data
foreach($data as $v)
list($val,$key) = explode('#',$v);
$arr[$key] .= "$val|";
$arr[$key] = $val;
So you just print out the array $arr to get the desired output.