How can the array_unique function be used to handle duplicate ShopIds in a PHP foreach loop?
When iterating over an array of data using a foreach loop in PHP, if there are duplicate ShopIds present, we can use the array_unique function to remove duplicates and ensure unique ShopIds are processed. By applying array_unique to the array of ShopIds before looping through them, we can avoid processing duplicate data and improve the efficiency of our code.
// Example array with duplicate ShopIds
$shopIds = [1, 2, 3, 2, 4, 1, 5];
// Remove duplicate ShopIds using array_unique
$uniqueShopIds = array_unique($shopIds);
// Loop through unique ShopIds
foreach ($uniqueShopIds as $shopId) {
// Process each unique ShopId
echo "Processing ShopId: $shopId\n";
}