合并单个数组的两个值

时间:2014-08-18 14:50:01

标签: php arrays multidimensional-array

如何组合多维数组的记录。我在数组合并之后通过array_merge合并了两个多维数组我得到了一个数组。在这个数组中,我有元素receivablesale_datepayablespurchase_date

这是我的阵列:

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 )
) 

现在我想检查sale_datepurchase_date是否相同,然后我在数组中获得receivablepayables。如果它们不相同那么我就会得到它。我已经尝试了很多东西,现在我已经撞墙了,不知道该怎么做。

这是我的愿望结果:

  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 )    
)  

这是我的PHP代码:

$query_sale = "SELECT sum( `sale_received` ) AS receivable, `sale_date`
               FROM `sale`
               GROUP BY `sale_date`
               ORDER BY `sale_date` ";
$result_sale = mysqli_query($con,$query_sale);

$query_purchase = "SELECT sum( `purchase_remaining` ) AS payables, `date`
                   FROM `purchase`
                   GROUP BY `date`
                   ORDER BY `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);

1 个答案:

答案 0 :(得分:2)

编写以这种方式返回数据的SQL查询可能更容易。

例如:

SELECT (CASE WHEN B.date IS NULL THEN A.sale_date ELSE B.date END) As date, (CASE WHEN payables IS NOT NULL THEN payables ELSE 0 END) As payables,
    CASE WHEN receivable IS NOT NULL THEN receivable ELSE 0 END) As receivable
FROM (SELECT sum( sale_received ) AS receivable, sale_date
           FROM sale
           GROUP BY sale_date) A
LEFT JOIN    ( SELECT sum( purchase_remaining ) AS payables, date
               FROM purchase
               GROUP BY date ) B
ON A.sale_date = B.date
UNION
SELECT (CASE WHEN B.date IS NULL THEN A.sale_date ELSE B.date END) As date, (CASE WHEN payables IS NOT NULL THEN payables ELSE 0 END) As payables,
    CASE WHEN receivable IS NOT NULL THEN receivable ELSE 0 END) As receivable
FROM (SELECT sum( sale_received ) AS receivable, sale_date
           FROM sale
           GROUP BY sale_date) A
RIGHT JOIN    ( SELECT sum( purchase_remaining ) AS payables, date
               FROM purchase
               GROUP BY date ) B
ON A.sale_date = B.date

这应该为两个表中的每个日期返回应付款和应收款的值,并且在特定日期只有一个值的情况下,另一个值为0。

在PHP中可能是这样的:

$query =     "SELECT (CASE WHEN B.date IS NULL THEN A.sale_date ELSE B.date END) As date, (CASE WHEN payables IS NOT NULL THEN payables ELSE 0 END) As payables, ".
"    CASE WHEN receivable IS NOT NULL THEN receivable ELSE 0 END) As receivable ".
"FROM (SELECT sum( sale_received ) AS receivable, sale_date ".
"           FROM sale ".
"           GROUP BY sale_date) A ".
"LEFT JOIN    ( SELECT sum( purchase_remaining ) AS payables, date ".
"               FROM purchase ".
"               GROUP BY date ) B ".
"ON A.sale_date = B.date ".
"UNION ".
"SELECT (CASE WHEN B.date IS NULL THEN A.sale_date ELSE B.date END) As date, (CASE WHEN payables IS NOT NULL THEN payables ELSE 0 END) As payables, ".
"    CASE WHEN receivable IS NOT NULL THEN receivable ELSE 0 END) As receivable ".
"FROM (SELECT sum( sale_received ) AS receivable, sale_date ".
"           FROM sale ".
"           GROUP BY sale_date) A ".
"RIGHT JOIN    ( SELECT sum( purchase_remaining ) AS payables, date ".
"               FROM purchase ".
"               GROUP BY 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);
}