note 68339 deleted from function.array-unique by danbrown

From: Date: Sun, 23 Jan 2011 17:23:25 +0000
Subject: note 68339 deleted from function.array-unique by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176195@lists.php.net to get a copy of this message
Note Submitter: a dot fotoglidis at 7mbit dot net ---- In addition to brettz9's remove_dups: This one will actually take a multi-dimensional array and return it minus the duplicates in regard to the specified element of the inner arrays. <?php /* Initialising new array to the first element of the given array. Check whether current element in initial array has already been added to new array. If yes break to save us some time. If no, then add current element to new array. */ function remove_dups($array, $row_element) { $new_array[0] = $array[0]; foreach ($array as $current) { $add_flag = 1; foreach ($new_array as $tmp) { if ($current[$row_element]==$tmp[$row_element]) { $add_flag = 0; break; } } if ($add_flag) $new_array[] = $current; } return $new_array; } // end function remove_dups ?> ______________________________________________ EXAMPLE <?php $array[0] = array('Charles','Wade',233); $array[1] = array('Charles','Watson',234); $array[2] = array('Tom','Wade',235); $array = remove_dups($array,0); ?> The above example will output: Array ( [0] => Array ([0] => 'Charles' [1] => 'Wade' [2] => 233) [1] => Array ([0] => 'Tom' [1] => 'Wade' [2] => 235) ) <?php remove dups($array,1); ?> will output: Array ( [0] => Array ([0] => 'Charles' [1] => 'Wade' [2] => 233) [1] => Array ([1] => 'Charles' [1] => 'Watson' [2] => 234) ) At each case it removes all inner arrays that have a duplicate in the specified position and keeps the first inner array only.

« previous php.notes (#176195) next »