当前位置:网站首页>Illuminate\support\collection de duplication unique list de duplication

Illuminate\support\collection de duplication unique list de duplication

2022-06-23 06:33:00 fareast_ mzh

list , Array Specify rule de duplication

curMsg->getFundId() == $prevMsg->getFundId()

Repeat post-processing

// Which element to delete ?

if ($curMsg->created_at > $prevMsg->created_at) {

      // The element to be deleted moves to the back
      self::swap($list, $i, $dupIdx);
}

The replaceable parts are as above

There is no order , be it so

  private static function swap(Collection $c, int $i, int $j) {
        $tmp = $c->get($i);
        $c->offsetSet($i, $c->get($j));
        $c->offsetSet($j, $tmp);
    }

    /**
     *  Remove fund_id repeat ,  identical fund_id Get the latest records , Delete old records 
     * @param Collection $list
     */
    private static function msgListUniq(Collection $list) {
        $n = $list->count();
        $countDel = 0;
        for ($i = 1; $i < $n; $i++) {
            $dupIdx = -1;
            /** @var $curMsg PayMessage */
            $curMsg = $list->get($i);
            if (empty($curMsg)) {
                break;
            }
            $prevMsg = null;
            for ($j = 0; $j < $i; $j++) {
                /** @var $prevMsg PayMessage */
                $prevMsg = $list->get($j);
                if ($curMsg->getFundId() == $prevMsg->getFundId()) {
                    $dupIdx = $j;
                    break;
                }
            }
            //  There is repetition fund_id
            if (0 <= $dupIdx) {
                if ($curMsg->created_at > $prevMsg->created_at) {
                    self::swap($list, $i, $dupIdx);
                }
                for ($k = $i+1; $k < $n-$countDel; $k++) {
                    $list->offsetSet($k-1, $list->get($k));
                }
                // $list->offsetUnset($k-1);
                $countDel += 1;
            }
        }
        //  Do not cycle unset
        while ($countDel > 0) {
            $list->offsetUnset($n - $countDel);
            $countDel -= 1;
        }
    }

原网站

版权声明
本文为[fareast_ mzh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230453322972.html