Last active
June 12, 2017 23:46
-
-
Save kosalvann/c6d01e0d7b2e86f399ceb241b3bd3ea2 to your computer and use it in GitHub Desktop.
Remove duplicate array items from the array list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result