Eliminating duplicating value in 2D array in php

50 Views Asked by At

2D Array

I can't attach images so please find array at link. I eliminated the duplicated value for this array. I used technique that is bit awful. Array name is $pap. Here is code

 `foreach ($pap as $key => $row)
 { 
 foreach ($row as $subkey => $subvalue)
 {       $p[$n]=$pap[$key][$subkey];
 $n++;
}
}
$unique = array_unique($p);
$n=0; $abc = array();
foreach ($unique as $key => $row){
$abc[$n]=$unique[$key]; $n++;
}
$m=0;
echo "<table align='center' border='2'>";
foreach ($pap as $key => $row)
{ 
echo "<tr>";
foreach ($row as $subkey => $subvalue)
{       
if($m<$n){
if($pap[$key][$subkey]==$abc[$m])   
{
echo "<td>";
echo $pap[$key][$subkey];
echo "</td>";
$m++;
}
}
}
echo "</tr>";
}
echo"</table>";`

Any better technique would be appreciated.

1

There are 1 best solutions below

0
On

Check this, I think you this will do the job for you.

<?php

$details = array( 
    0 => array("id"=>"1", "name"=>"Mike",    "num"=>"9876543210"), 
    1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"), 
    2 => array("id"=>"1", "name"=>"Mathew",  "num"=>"784581254"), 
); 

function super_unique($array)
{
  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

  foreach ($result as $key => $value)
  {
    if ( is_array($value) )
    {
      $result[$key] = super_unique($value);
    }
  }

  return $result;
}

var_dump(super_unique($details));
?>