I have an array which is 2D and I want to remove duplicates from it,
the var dump look like this 1=>abc.com. 2=>acc.com, 3=>abc.com
so I want to remove the second occurence of abc.com, that is I want to remove 3=>abc.com
,
I tried to use the nested loop, but the code is not working.
foreach ($var as $m)
{
foreach ($var as $s)
{
if(isset($m['Email'])){
if($m['Email'] == $s['Email']){
echo 'matched with '.$s['Email'];
echo '</br>';
unset($s);
//echo $v['Email'];
//echo "<br>";
}
}
}
}
Am I missing something?
You can just use the array_unique function
Edit: Here is the code and output from Here (Codepad)
PHP
<?php
$input = array("1" => "abc.com", "2" => "acc.com", "3" => "abc.com");
$result = array_unique($input);
print_r($result);
?>
Output
Array
(
[1] => abc.com
[2] => acc.com
)
Edit2: To have it remove duplicates from a specific column, just use
$output = array_intersect_key($input, array_unique(array_column($input, 'Email')));
Where $input is the complete array and 'Email' is the column you wish to remove duplicates from
array_unique($array);
you also have a mistake in your nested foreach. Assuming what you posted above, you only need one foreach loop. If you need to add a 2nd, it should be based on the temp variables you are setting e.g. foreach($table as $row => $key) .. you can do foreach($key as $item)
etc!
If you only want to remove the duplicates you can use array_unique,
$arr = array(
1 => "abc.com",
2 => "acc.com",
3 => "abc.com"
);
$result = array_unique($arr);
print_r($result);