Quantcast
Channel: Sort based on multiple criteria on keys and values - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Sort based on multiple criteria on keys and values

$
0
0

I would like to sort the array below using PHP. The main problem is that I need to sort the array based on multiple criteria:

  • first show the websites with a price
  • if the price is the same, sort them alphabetically
  • if multiple sites have no price, sort them alphabetically

So this array

array(
    [Beslist.nl] => Array
        (
            [price] => 141,63
        )

    [Wehkamp.nl] => Array
        (
            [price] => none
        )

    [Bol.com] => Array
        (
            [price] => none
        )

    [Zalando.nl] => Array
        (
            [price] => none
        )

    [Webwinkel.nl] => Array
        (
            [price] => none
        )

    [Overig.nl] => Array
        (
            [price] => none
        )
)

Should be sorted like this:

array(
    [Beslist.nl] => Array
        (
            [price] => 141,63
        )

    [Bol.com] => Array
        (
            [price] => none
        )

    [Overig.nl] => Array
        (
            [price] => none
        )

    [Webwinkel.nl] => Array
        (
            [price] => none
        )

    [Wehkamp.nl] => Array
        (
            [price] => none
        )

    [Zalando.nl] => Array
        (
            [price] => none
        )

)

I tried asort and ksort, but I need to sort based on multiple criteria, which makes it more complex. I was hoping I could sort the records using SQL (when I read the records from the database). However, the price needs to be calculated afterwards; that is why I need to use PHP.

Anyone who can help me out?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images