php - mutlplying values from one array with values in another array -
i'm working 2 databases. 1 database on microsoft sql 2012 , other on mysql 5.5. microsoft sql server has orders table , mysql has 3 currency exchange rate tables. orders table has orders 6 different countries; however, orders 3 of countries have sales totals in local currencies (non-u.s.). 3 currency exchange rates tables separated country containing daily rates. using php, need current years total sales grouped month , country , have exchange rates applied countries total sales not in u.s. currency. each months exchange rate needs average rate month.
i have 2 arrays below. first array orders have sales summed , grouped month , country. second array currency exchange rate has average exchange rates calculated , grouped month , country.
how multiply total sales each month exchange rate countries not in u.s. currency, kr, jp, , nz?
orders
$query = $db->prepare("select month(us_orderdate) month, a.country, sum(a.sales) total_sales ( select case when country = 'kr' dateadd(day, +1, cast(orderdate date)) else cast(orderdate date) end us_orderdate, country, sum(total) sales orders (orderstatusid in ('7','8','9') , country = 'kr') or (orderstatusid in ('7','8','9') , country = 'jp') or (orderstatusid in ('7','8','9') , country = 'au') or (orderstatusid in ('7','8','9') , country = 'nz') or (orderstatusid in ('7','8','9') , country = 'us') or (orderstatusid in ('7','8','9') , country = 'ca') group cast(orderdate date),country) year(us_orderdate) = year(getdate()) group month(us_orderdate), a.country order month(us_orderdate) asc "); try { $query->execute(); $results = $query->fetchall(pdo::fetch_assoc); } catch (pdoexception $e) { die("{error: {$e->getmessage()}}"); } array(54) { [0]=> array(3) { ["month"]=> string(1) "1" ["country"]=> string(2) "au" ["total_sales"]=> string(7) "9095.70" } [1]=> array(3) { ["month"]=> string(1) "1" ["country"]=> string(2) "ca" ["total_sales"]=> string(9) "113993.00" } [2]=> array(3) { ["month"]=> string(1) "1" ["country"]=> string(2) "kr" ["total_sales"]=> string(7) "9284.75" } [3]=> array(3) { ["month"]=> string(1) "1" ["country"]=> string(2) "nz" ["total_sales"]=> string(6) "883.25" } [4]=> array(3) { ["month"]=> string(1) "1" ["country"]=> string(2) "us" ["total_sales"]=> string(9) "609538.25" } [5]=> array(3) { ["month"]=> string(1) "2" ["country"]=> string(2) "au" ["total_sales"]=> string(7) "7393.65" } [6]=> array(3) { ["month"]=> string(1) "2" ["country"]=> string(2) "ca" ["total_sales"]=> string(9) "100279.43" } [7]=> array(3) { ["month"]=> string(1) "2" ["country"]=> string(2) "jp" ["total_sales"]=> string(9) "916110.00" } [8]=> array(3) { ["month"]=> string(1) "2" ["country"]=> string(2) "kr" ["total_sales"]=> string(7) "5217.55" } [9]=> array(3) { ["month"]=> string(1) "2" ["country"]=> string(2) "nz" ["total_sales"]=> string(7) "1355.85" } [10]=> array(3) { ["month"]=> string(1) "2" ["country"]=> string(2) "us" ["total_sales"]=> string(9) "616422.11" } ....
exchange rates
$query_exchange_rate = $db_exchange_rate->prepare("select month(date) month, 'nz' country, round((sum(rate)/count(rate)),5) avg_rate currency_exchange_rates.new_zealand year(date) = year(curdate()) group month(date) union select month(date) month, 'jp' country, round((sum(rate)/count(rate)),5) avg_rate currency_exchange_rates.japan year(date) = year(curdate()) group month(date) union select month(date) month, 'kr' country, round((sum(rate)/count(rate)),5) avg_rate currency_exchange_rates.south_korea year(date) = year(curdate()) group month(date) order month asc"); try { $query_exchange_rate->execute(); $results_exchange_rate = $query_exchange_rate->fetchall(pdo::fetch_assoc); } catch (pdoexception $e) { die("{error: {$e->getmessage()}}"); } array(27) { [0]=> array(3) { ["month"]=> string(1) "1" ["country"]=> string(2) "nz" ["avg_rate"]=> string(7) "0.76614" } [1]=> array(3) { ["month"]=> string(1) "1" ["country"]=> string(2) "jp" ["avg_rate"]=> string(7) "0.00845" } [2]=> array(3) { ["month"]=> string(1) "1" ["country"]=> string(2) "kr" ["avg_rate"]=> string(7) "0.00090" } [3]=> array(3) { ["month"]=> string(1) "2" ["country"]=> string(2) "nz" ["avg_rate"]=> string(7) "0.74309" } [4]=> array(3) { ["month"]=> string(1) "2" ["country"]=> string(2) "jp" ["avg_rate"]=> string(7) "0.00842" } [5]=> array(3) { ["month"]=> string(1) "2" ["country"]=> string(2) "kr" ["avg_rate"]=> string(7) "0.00090" }
just loop through them. simplistic way loop through long one, looping through short 1 each time.
foreach($results &$a) // need reference update foreach($results_exchange_rate $b) if($a['month'] == $b['month'] && $a['country']==$b['country']) $a['total_sales']*= $b['avg_rate'];
that multiply total sales avg rate every time month , country same in both arrays.
Comments
Post a Comment