0

Question:

What is the best PHP 7+ approach for sorting the following array based on the array instance (string) value of array->status.

Goal:

Re-order the array based on the status value. If the value is "StatusAvailable", I would want to move the array instance to the top of the array. Since the array may contain multiple instances where a status of "StatusAvailable" is defined, the next ordering condition fallback would be the date value.

Array
(
   [0] => stdClass Object
       (
           [date]   => 1432875600000
           [value1] => TEXT
           [value2] => TEXT
           [value3] => TEXT
           [value4] => TEXT
           [value5] => TEXT
           [status] => 
           [value6] => TEXT
           [value7] =>
       )

    [1] => stdClass Object
       (
           [date]   => 1432875600000
           [value1] => TEXT
           [value2] => TEXT
           [value3] => TEXT
           [value4] => TEXT
           [value5] => TEXT
           [status] => StatusAvailable
           [value6] => TEXT
           [value7] =>
       )


    [2] => stdClass Object
       (
           [date]   => 14472543800000
           [value1] => TEXT
           [value2] => TEXT
           [value3] => TEXT
           [value4] => TEXT
           [value5] => TEXT
           [status] => SomeOtherStatus
           [value6] => TEXT
           [value7] =>
       )

    [3] => stdClass Object
       (
           [date]   => 1467349200000
           [value1] => TEXT
           [value2] => TEXT
           [value3] => TEXT
           [value4] => TEXT
           [value5] => TEXT
           [status] => StatusAvailable
           [value6] => NO
           [value7] =>
       )

    # the array list can/will continue at random length
)
Joey O
  • 315
  • 2
  • 17
  • [usort](http://php.net/usort). – Jonnix Oct 26 '18 at 14:29
  • How do you sort when it's not StatusAvailable? which comes first SomeOtherStatus or NoStatus? Why don't you turn your status parameter into a map having `['label'=> StatusAvailable,'sortOrder'=>1]` then you can easily use usort on sortOrder – Hirad Roshandel Oct 26 '18 at 14:30
  • The array is delivered from an API, pre-formatted. The exercise would be to re-order what we received based on the "status" of each instance, then, it multiple are returned, the next sorting should fall back on the "date". Hope that makes sense! @HiradRoshandel – Joey O Oct 26 '18 at 14:32
  • @JonStirling, the goal of this post was to archive demonstrations of recommendations based on opinion or performance. I'm aware of `usort`, however, I would prefer to have demonstrations aligned with modern standards presented. Thanks for providing the reference link however! – Joey O Oct 26 '18 at 14:34
  • @Joey Then I'm afraid you might have the wrong idea about SO. For example, asking for things as a matter of opinion is explicitly off-topic. Might be worth read the help section :). – Jonnix Oct 26 '18 at 14:37
  • @GeorgeAppleton I was referring directly to OPs comment to me, which stated `the goal of this post was to archive demonstrations of recommendations based on opinion ......` – Jonnix Oct 26 '18 at 15:16

0 Answers0