1

My array:

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

I want to be able to echo out the [month] and not have it duplicated, but still associate the other values to the correct month. To get to this point I have done an array_merge to put them together like you see them.

Separately they look like this:

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

I have tried array_unique and that does not work. I am using a foreach statement to echo out the values.

Thank you!

The SQL Queries:

$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;
 }
Keith Holm
  • 11
  • 3
  • Use `foreach` to create new array when the month name is a key. – biesior Aug 03 '20 at 19:57
  • How did you use array_unique? That should work. – Don't Panic Aug 03 '20 at 20:00
  • @Don't Panic -- $combined = array_merge($array1, $array2); $combined1 = array_unique($combined); print_r($combined1); Results: Array ( [0] => Array ( [month] => November [Average Purchase Price] => 2.52 [Total Purchase Gallons] => 84000 ) ) – Keith Holm Aug 03 '20 at 20:30
  • @biesior can you give me an example? I am already using a foreach -- foreach($combined as $purchase){ – Keith Holm Aug 03 '20 at 20:32
  • #biesior I am getting this from 2 SQL statements, and am trying to put them together. – Keith Holm Aug 03 '20 at 20:34
  • array_unique will work, but you need to use it just on that column rather than the entire array of arrays. Like `array_unique(array_column($combined, 'month'))`. – Don't Panic Aug 03 '20 at 20:38
  • @Don't Panic -- That did not work. Maybe I need to explain it better. I am using two SQL statements to get this info, then I merge the 2 arrays. -- I then want to echo the combined array out showing the following in a , -- Month | Purchase Price | Purchase Gallons | Customer Price | Customer Gallons -- Month2 | Purchase Price | Purchase Gallons | Customer Price | Customer Gallons – Keith Holm Aug 03 '20 at 21:11
  • Ah sorry, I misread it. I thought you were wanting the unique months only. – Don't Panic Aug 03 '20 at 21:18

2 Answers2

0

That's easy iteration:

<?php

$oldArr = [
    ['month' => 'November', 'Average Purchase Price' => 2.52, 'Total Purchase Gallons' => 84000],
    ['month' => 'October', 'Average Purchase Price' => 2.615, 'Total Purchase Gallons' => 63000],
    ['month' => 'November', 'Average Customer Price' => 2.79, 'Total Customer Gallons' => 25000],
    ['month' => 'October', 'Average Customer Price' => 2.9050000000000002, 'Total Customer Gallons' => 5500]
];

$newArr = [];
foreach ($oldArr as $item) {
    $month = $item['month'];
    if (!array_key_exists($month, $newArr)) {
        $newArr[$month] = $item;
    }
}

echo '<pre>';
print_r($newArr);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
biesior
  • 55,576
  • 10
  • 125
  • 182
0

From your original two arrays, just re-index on month and recursively merge them:

$result = array_merge_recursive(array_column($array1, null, 'month'),
                                array_column($array2, null, 'month'));

Which yields:

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

Making it easy to access any key from a month:

echo $result['October']['Average Purchase Price'];
echo $result['October']['Average Customer Price'];
// etc...

Or loop:

foreach($result as $month => $values) {
    echo $month;
    echo $values['Average Purchase Price'];
    echo $values['Average Customer Price'];
    // etc...
}

However, you edited in two queries that can be combined. This may work or ask another question and someone can undoubtedly give you one query:

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
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87