Link to home
Start Free TrialLog in
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/questions/28427438/Filter-MySQL-results-by-max-value-for-each-24-hour-cycle-using-PHP.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

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of williamskellorn
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.
I would definitely add the DATETIME column.  Since you're working with daily information, it will save a lot of headaches!
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?
@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?