我的阵列:
Array (
[0] => Array ( [month] => November [Average Purchase Price] => 2.52 [Total Purchase Gallons] => 84000 )
[1] => Array ( [month] => October [Average Purchase Price] => 2.615 [Total Purchase Gallons] => 63000 )
[2] => Array ( [month] => November [Average Customer Price] => 2.79 [Total Customer Gallons] => 25000 )
[3] => Array ( [month] => October [Average Customer Price] => 2.9050000000000002 [Total Customer Gallons] => 5500 ) )
我希望能够回显[month]并且不复制它,但是仍然将其他值与正确的月份相关联。为了达到这一点,我做了一个数组合并,把它们像你看到的那样放在一起。
它们分别如下所示:
1
Array (
[0] => Array ( [month] => November [Average Purchase Price] => 2.52 [Total Purchase Gallons] => 84000 )
[1] => Array ( [month] => October [Average Purchase Price] => 2.615 [Total Purchase Gallons] => 63000 ) )
2
Array (
[0] => Array ( [month] => November [Average Customer Price] => 2.79 [Total Customer Gallons] => 25000 )
[1] => Array ( [month] => October [Average Customer Price] => 2.9050000000000002 [Total Customer Gallons] => 5500 ) )
我试过你的独特,这是行不通的。我使用foreach语句来呼应这些值。
谢谢您!
sql查询:
$sql = "SELECT month, AVG(price) AS 'Average Purchase Price', SUM(gallons) as 'Total Purchase Gallons' from purchase_contracts
group BY month";
$purch = mysqli_query($con, $sql) or die(mysqli_error($con));
while ($rows = mysqli_fetch_assoc($purch))
{
$purch_items[] = $rows;
}
$sql1 = "SELECT month, AVG(price) AS 'Average Customer Price', SUM(gallons) as 'Total Customer Gallons' from customer_contracts
group BY month";
$cust = mysqli_query($con, $sql1) or die(mysqli_error($con));
while ($rows1 = mysqli_fetch_assoc($cust))
{
$cust_items[] = $rows1;
}
2条答案
按热度按时间yc0p9oo01#
这很简单:
izj3ouym2#
从原来的两个数组中,只需重新索引
month
并递归合并它们:由此产生:
使您可以轻松地访问一个月内的任何密钥:
或循环:
但是,您在两个可以组合的查询中进行了编辑。这可能有用,也可能会问另一个问题,毫无疑问,有人会问你一个问题: