Skip to content
Advertisement

Remove Duplicates in an array in php

My array:

JavaScript

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

JavaScript

#2

JavaScript

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:

JavaScript

Advertisement

Answer

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

JavaScript

Which yields:

JavaScript

Making it easy to access any key from a month:

JavaScript

Or loop:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement