php - Combine two values of a single array -
how can combine records of multidimensional array. have merge 2 multidimensional arrays through array_merge
after array merge got single array. in array have elements receivable
, sale_date
, payables
and purchase_date
.
this array:
array ( [0] => array ( [receivable] => 20 [sale_date] => 2014-06-24 ) [1] => array ( [receivable] => 400 [sale_date] => 2014-06-28 ) [2] => array ( [receivable] => 3410 [sale_date] => 2014-07-04 ) [3] => array ( [receivable] => 923 [sale_date] => 2014-07-07 ) [4] => array ( [receivable] => 1203 [sale_date] => 2014-08-15 ) [5] => array ( [payables] => 9800 [purchase_date] => 2014-06-23 ) [6] => array ([payables] => 0 [purchase_date] => 2014-06-24 ) [7] => array ( [payables] => 16155 [purchase_date] => 2014-06-28 ) [8] => array ( [payables] => 993 [purchase_date] => 2014-08-18 ) )
now want check if sale_date
, purchase_date
same receivable
, payables
in array. if not same is. have heve tryed many thing have hit wall d don't know do.
this desire result:
array ( [0] => array ( [purchase_date] => 2014-06-24 [sale_date] => 2014-06-24 [receivable] => 20 [payables] => 0 ) [1] => array ( [sale_date] => 2014-06-28 [purchase_date] => 2014-06-28 [payables] => 16155 [receivable] => 400 ) [2] => array ( [receivable] => 3410 [sale_date] => 2014-07-04 ) [3] => array ( [receivable] => 923 [sale_date] => 2014-07-07 ) [4] => array ( [receivable] => 1203 [sale_date] => 2014-08-15 ) [5] => array ( [payables] => 9800 [purchase_date] => 2014-06-23 ) [6] => array ( [payables] => 993 [purchase_date] => 2014-08-18 ) )
this php code:
$query_sale = "select sum( `sale_received` ) receivable, `sale_date` `sale` group `sale_date` order `sale_date` "; $result_sale = mysqli_query($con,$query_sale); $query_purchase = "select sum( `purchase_remaining` ) payables, `date` `purchase` group `date` order `date`"; $result_purchase = mysqli_query($con,$query_purchase); $array_receivable = array(); $array_payables = array(); while($sale = mysqli_fetch_array($result_sale)){ $receivable = $sale['receivable']; $sale_date = $sale['sale_date']; $array_receivable[] = array('receivable' => $receivable , 'sale_date' => $sale_date); } while($purchase = mysqli_fetch_array($result_purchase)){ $payables = $purchase['payables']; $purchase_date = $purchase['date']; $array_payables[] = array('payables' => $payables , 'purchase_date' => $purchase_date); } $both_array = array_merge($array_receivable , $array_payables);
it may easier write sql query returns data in way..
for example:
select (case when b.date null a.sale_date else b.date end) date, (case when payables not null payables else 0 end) payables, case when receivable not null receivable else 0 end) receivable (select sum( sale_received ) receivable, sale_date sale group sale_date) left join ( select sum( purchase_remaining ) payables, date purchase group date ) b on a.sale_date = b.date union select (case when b.date null a.sale_date else b.date end) date, (case when payables not null payables else 0 end) payables, case when receivable not null receivable else 0 end) receivable (select sum( sale_received ) receivable, sale_date sale group sale_date) right join ( select sum( purchase_remaining ) payables, date purchase group date ) b on a.sale_date = b.date
this should return value both payables , receivables every date in both tables, , there 1 value particular date, other 1 0.
maybe in php:
$query = "select (case when b.date null a.sale_date else b.date end) date, (case when payables not null payables else 0 end) payables, ". " case when receivable not null receivable else 0 end) receivable ". "from (select sum( sale_received ) receivable, sale_date ". " sale ". " group sale_date) ". "left join ( select sum( purchase_remaining ) payables, date ". " purchase ". " group date ) b ". "on a.sale_date = b.date ". "union ". "select (case when b.date null a.sale_date else b.date end) date, (case when payables not null payables else 0 end) payables, ". " case when receivable not null receivable else 0 end) receivable ". "from (select sum( sale_received ) receivable, sale_date ". " sale ". " group sale_date) ". "right join ( select sum( purchase_remaining ) payables, date ". " purchase ". " group date ) b ". "on a.sale_date = b.date"; $result = mysqli_query($con,$query); $both_array = []; while($row = mysqli_fetch_array($result)){ $date = $row['date']; $receivable = $row['receivable']; $payable = $row['payables']; $both_array[] = array('date' => $date , 'receivable' => $receivable, 'payable' => $payables); }
Comments
Post a Comment