We help IT Professionals succeed at work.
Get Started

Group and filter PHP Array of stdClass objects

2,146 Views
Last Modified: 2014-05-21
This question related to previosuly opned question - https://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28427438.html  - thought best to keep separate as this question assumes a PHP-only approach following an initial query as an alternative to multiple queries.

I have a query which returns the array of stdClass objects below. This is a sample, in reality the query would return hundreds of thousands of entries in the array.

I would like to remove most array entries / objects so that I am left with one entry for each ptt value on each loc_date, choosing the entry that has the highest max_location_class value to keep.

E.g. Lose entry 0 and keep entry 1 as they both have the same ptt and loc_date values, but entry 1 has the higher value for max_location_class.

The expected outcome starting with the array below would leave entries 1, 5 and 7, still in ptt / loc_date order.

I presume I need to group entries that have matching ptt and loc_date values and then filter, but am a bit perplexed as to how to approach this with the array contents being stdClass Objects.

I will then need to print out the values of the filtered entries objects in HTML.

Thanks in advance for any help.

Array
(
    [0] => stdClass Object
        (
            [loc_date] => 2014-04-20
            [max_location_class] => 1
            [ptt] => 128303
        )

    [1] => stdClass Object
        (
            [loc_date] => 2014-04-20
            [max_location_class] => 2
            [ptt] => 128303
        )

    [2] => stdClass Object
        (
            [loc_date] => 2014-04-21
            [max_location_class] => 1
            [ptt] => 128303
        )

    [3] => stdClass Object
        (
            [loc_date] => 2014-04-21
            [max_location_class] => 1
            [ptt] => 128303
        )

    [4] => stdClass Object
        (
            [loc_date] => 2014-04-21
            [max_location_class] => 1
            [ptt] => 128303
        )

    [5] => stdClass Object
        (
            [loc_date] => 2014-04-21
            [max_location_class] => 3
            [ptt] => 128303
        )

    [6] => stdClass Object
        (
            [loc_date] => 2014-04-21
            [max_location_class] => 1
            [ptt] => 128304
        )

    [7] => stdClass Object
        (
            [loc_date] => 2014-04-21
            [max_location_class] => 2
            [ptt] => 128304
        )

)

Open in new window

Comment
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE