Skip to content

Commit

Permalink
Update PhpCollection.php
Browse files Browse the repository at this point in the history
  • Loading branch information
izica committed Apr 16, 2019
1 parent e553306 commit 41a0c29
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions PhpCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,13 @@ public function sortBy($key, $asc = true)
* @param $function
* @return PhpCollection
*/
public function sort($function)
public function sort($function = false)
{
if ($function === false) {
$function = function ($a, $b) {
return $a > $b ? +1 : -1;
};
}
$arData = $this->arCollection;
uasort($arData, $function);
return new PhpCollection($arData);
Expand Down Expand Up @@ -375,25 +380,25 @@ public function merge($array)
}

/**
* @param $array
* @return PhpCollection
*/
public function unique()
{
$arBuffer = [];
$arResult = [];
foreach ($this->arCollection as $arItem){
$sHash = md5($arItem);
if(isset($arBuffer[$sHash])){
if($arBuffer[$sHash] !== json_encode($arItem)){
foreach ($this->arCollection as $arItem) {
$sJson = json_encode($arItem);
$sHash = md5($sJson);
if (isset($arBuffer[$sHash])) {
if ($arBuffer[$sHash] !== $sJson) {
$arResult[] = $arItem;
}
}else{
$arBuffer[$sHash] = json_encode($arItem);
} else {
$arBuffer[$sHash] = $sJson;
$arResult[] = $arItem;
}
}

return new PhpCollection($arResult);
}
}
}

0 comments on commit 41a0c29

Please sign in to comment.