Skip to content

Instantly share code, notes, and snippets.

@kosalvann
Last active June 12, 2017 23:46
Show Gist options
  • Save kosalvann/c6d01e0d7b2e86f399ceb241b3bd3ea2 to your computer and use it in GitHub Desktop.
Save kosalvann/c6d01e0d7b2e86f399ceb241b3bd3ea2 to your computer and use it in GitHub Desktop.
Remove duplicate array items from the array list
<?php
/**
* Remove duplicates found in a list of array
*/
class RemoveDuplicates
{
/**
* Declare properties
*
* @var array $data_array The list of array
*/
public $data_array;
/**
* The list of array
*/
public function __construct($data_array = [])
{
$this->data_array = $data_array;
$this->sortArray($data_array);
$this->removeDuplicatesFromArray($data_array);
}
/**
* Sort the list of array from ascending order
*
* @param array $arg Return ascending sorted array from list
* @return array
*/
public function sortArray($data_array)
{
return asort($this->data_array);
}
/**
* Remove duplicate array from list
* @param array $data_array [description]
* @return array [description]
*/
public function removeDuplicatesFromArray($data_array)
{
$data_array = array_map("unserialize", array_unique(array_map("serialize", $data_array)));
foreach ($data_array as $key => $val) {
return $this->key . ' ' . $this->val;
}
}
}
$newlist = new RemoveDuplicates;
$listOfArray = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'c' => 3, 'a' => 1);
// print_r($listOfArray);
echo $newlist->removeDuplicatesFromArray($listOfArray);
@kosalvann
Copy link
Author

Result

One (1)

Two (2)

Three (3)

Four (4)

Five (5)

Six (6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment