foreach ($this->data as $d){
//array for 'prices'
$result = [];
$p_id = $d['product_id'];
foreach($d['prices'] as $key => $p){
$p_purchase = $p['price_purchase'];
$p_selling = $p['price_selling'];
$p_discount = $p['price_discount'];
$sel = "select * from Products where product_id=$p_id and region=$key;";
$smth = $this->bd_connect->query($sel);
//for dublicate values
if ($smth->num_rows!=0){
$sql = "update Products set prices_purchase=$p_purchase, prices_selling=$p_selling, prices_discount=$p_discount where product_id=$p_id and region=$key;";
}
//for without dublicate values
else{
$sql = "INSERT INTO Products (product_id, region, prices_purchase, prices_selling, prices_discount)
VALUES ('$p_id', '$key', '$p_purchase', '$p_selling', '$p_discount')";
}
//inserting values into table Products
if($this->bd_connect->query($sql)==FALSE){
die('Error: '.$this->bd_connect->error);
}
//json format array
$new = array_push($result,array("$key"=>['price_purchase'=>$p_purchase, 'price_selling'=>$p_selling, 'price_discount'=>$p_discount]));
}
$arr = array('product_id'=>$p_id, 'prices'=>$result);
print_r(json_encode($arr, JSON_PRETTY_PRINT));
}