Monday, 6 October 2014

Remove duplicated values and exactly UNIQUE from multidimensional array using PHP

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
echo "Hey welcome";
$avar = array(
0 => array(1,2,3,4,5,6,7,8,9),
1 => array(10,11,12,13,14,15,16,17,7,8,9,10),
23 => array(21,22,23,4,5,6,7,11,12,13,14,15,21));

$seen = array();
foreach($avar as &$entry){
    $entry = array_unique(array_diff($entry,$seen));
    $seen = array_merge($entry,$seen);
}
unset($entry);
var_dump($avar);

?>

Required Output:
$avar = array( 
0 => array(1,2,3,4,5,6,7,8,9), 
1 => array(10,11,12,13,14,15,16,17), 
23 => array(21,22,23));

0 comments:

Post a Comment