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

Answer by deceze for Sort based on multiple criteria on keys and values

$
0
0

There are perfectly suitable approaches detailed here, but considering the actual structure of your array, this probably requires a bit more explanation. Specifically, here's how you can sort by both keys and values:

uksort($array, function ($siteA, $siteB) use ($array) {
    $priceA = $array[$siteA]['price'];
    $priceB = $array[$siteB]['price'];

    if ($priceA == $priceB) {
        return strcmp($siteA, $siteB);
    }
    if (!$priceB) {
        return -1;
    }
    if (!$priceA) {
        return 1;
    }
    return $priceB - $priceA;
});

You might need to adjust the specific comparisons and return logic here, but this illustrates the approach.


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>