-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")

3
  • In one part of your code $mainNews is an array, in another it's an object. I don't see where $mainNewsLinks is initialised. It seems likely that you've confused a variable name somewhere. Commented Jun 16, 2021 at 11:13
  • $mainnewsLinks is initialized in repo: public function store(array $mainNewsLinks, int $sectorId): array Commented Jun 16, 2021 at 11:15
  • @UmairBasit did the proposed solution work for you? Commented Jul 2, 2021 at 0:02

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.