-1

I am getting duplicate data in an array and that data is storing in my DB. I am using array_unique to refine the duplicate data but it is not working. Please tell me is there any other way to make data unique and store in DB this way.

if (preg_match($keywords, $links[$i]->href)) {
    if (filter_var($links[$i]->href, FILTER_VALIDATE_URL) !== false) {
        array_push($mainNews, $links[$i]->href);
    }
}
    return (array_unique($mainNews));

Error I am getting:

Undefined array key 1 at C:\xampp\htdocs\pacra-crawlers\modules\crawlers\services\MainNewsRepository.php:46

    for ($i = 0; $i < count($mainNewsLinks); $i++) {
        $mainNews = new MainNews();
        $mainNews->newspaper_id = $this->newspaperId;
        $mainNews->sector_id = $sectorId;
        $mainNews->url = $mainNewsLinks[$i];
        $mainNews->save();
    }
    return ['status' => true];
}

C:\xampp\htdocs\pacra-crawlers\modules\crawlers\services\MainNewsRepository.php:46 Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined array key 1", "C:\xampp\htdocs\pacra-crawlers\modules\crawlers\services\MainNewsRepo sitory.php")

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

1 Answers1

1

array_unique is working however although it is removing duplicates it is maintaining the same keys i.e.

If you had the following items in an array with

position/key value
0         a
1         a
2         b

array_unique would return

position/key value
0         a
2         b

which is why you are getting the Undefined array key when looping through the array based on the incrementing index $i.

Based on your sample you could use a foreach loop since you are only interested in the value eg

   foreach($mainNewsLinks as $mainNewsLink) {
        $mainNews = new MainNews();
        $mainNews->newspaper_id = $this->newspaperId;
        $mainNews->sector_id = $sectorId;
        $mainNews->url = $mainNewsLink;
        $mainNews->save();
    }
    

If you would like to continue indexing or iterating through each element based on an index, you could use array_values in your return eg


return array_values(array_unique($mainNews));

from your function to reset the array keys to incrementing indexes

ggordon
  • 9,790
  • 2
  • 14
  • 27