Avatar of williamskellorn
williamskellorn
 asked on

Group and filter PHP Array of stdClass objects

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

PHPProgramming Languages-OtherScripting Languages

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Julian Hansen

Any reason you are not doing this in the query itself

SELECT MAX(max_location_class), ptt, loc_date FROM Birds GROUP BY ptt, loc_date;

Open in new window

ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
williamskellorn

ASKER
Brilliant - thanks.

Following further consideration I've decided to pursue adding  a column in the db that converts the location_date timestamp to date time to allow the solution linked to at the top of the post to work - but it's good to know I've got a back-up plan that doesn't require further queries.
Ray Paseur

I would definitely add the DATETIME column.  Since you're working with daily information, it will save a lot of headaches!
Your help has saved me hundreds of hours of internet surfing.
fblack61
Julian Hansen

Any reason why you don't do this in a query?

Seems like an over complication to do it in code when the database provides that functionality already?
Ray Paseur

@julianH: This appears to be part of a larger dialog that is accompanied by legacy data structures, and it's clear to me that we don't have all of the information or the moving parts needed to give comprehensive answers to these questions.  In this case the author asked for a PHP-only solution.  Who knows why?