Skip to content
Advertisement

Remove Duplicates in an array in php

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;
 }

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement