내 어레이 :
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 ) )
나는 [월]을 반향하고 복제하지 않고 다른 값을 올바른 월에 연결하고 싶습니다. 이 지점에 도달하기 위해 나는 당신이 보는 것처럼 그것들을 모으기 위해 array_merge를 수행했습니다.
별도로 그들은 다음과 같이 보입니다.
#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 ) )
나는 array_unique를 시도했지만 작동하지 않습니다. 값을 에코하기 위해 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;
}
원래 두 배열에서 다시 색인화 month
하고 재귀 적으로 병합하십시오.
$result = array_merge_recursive(array_column($array1, null, 'month'),
array_column($array2, null, 'month'));
결과 :
Array
(
[November] => Array
(
[month] => Array
(
[0] => November
[1] => November
)
[Average Purchase Price] => 2.52
[Total Purchase Gallons] => 84000
[Average Customer Price] => 2.79
[Total Customer Gallons] => 25000
)
[October] => Array
(
[month] => Array
(
[0] => October
[1] => October
)
[Average Purchase Price] => 2.615
[Total Purchase Gallons] => 63000
[Average Customer Price] => 2.905
[Total Customer Gallons] => 5500
)
)
한 달 동안 모든 키에 쉽게 액세스 할 수 있습니다.
echo $result['October']['Average Purchase Price'];
echo $result['October']['Average Customer Price'];
// etc...
또는 루프 :
foreach($result as $month => $values) {
echo $month;
echo $values['Average Purchase Price'];
echo $values['Average Customer Price'];
// etc...
}
그러나 결합 할 수있는 두 개의 쿼리에서 편집했습니다. 이것은 효과가 있거나 다른 질문을 할 수 있으며 누군가 의심 할 여지없이 한 가지 질문을 줄 수 있습니다.
SELECT month, AVG(price) AS 'Average Purchase Price', SUM(gallons) AS 'Total Purchase Gallons'
FROM purchase_contracts GROUP BY month
UNION ALL
SELECT month, AVG(price) AS 'Average Customer Price', SUM(gallons) AS 'Total Customer Gallons'
FROM customer_contracts GROUP BY month
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다