I have 2 arrays of objects in a e-commerce project (built in Code Igniter), one an array of product objects and one an array of sale item objects, with product id’s and the amount of discount. I need to compare the arrays so I can put a new price in the items which are on sale, but I cannot seem to code the loops properly. Here is my current code:
private function checkSalesProducts($query) { //$query is an array of product objects
$this->db->select("sale_product, sale_discount");
$salesItems = $this->db->get("sale_items");
foreach($salesItems->result() as $salesItem)
{
for($i=0; sizeof($query); ++$i)
{
if($salesItem->sales_product == $query[$i]->prod_id)
{
$query[$i]->prod_price = $query[$i]->prod_price * (1 - $salesItem->sales_discount);
$query[$i]->sale_item = true;
break;
}
}
}
echo "<pre>";
print_r($query);
echo "</pre>";
}
Any ideas?
Your
for()should beYour script should freeze at the moment since you tell it to loop while
sizeof($query) > 0, which it always have since you don’t modify it in the loop.