2

I have the following array:

Array
(
[3698] => Array
    (
        [brand] => Brand 1
        [rate] => 198
        [availability] => 0
        [stopsales] => 0
        [conditions] => 1
        [currencycode] => 1
    )

[1805] => Array
    (
        [brand] => Brand 2
        [rate] => 200,6
        [availability] => 0
        [stopsales] => 0
        [conditions] => 1
        [currencycode] => 1
    )

[1801] => Array
    (
        [brand] => Brand 3
        [rate] => 202,5
        [availability] => 0
        [stopsales] => 0
        [conditions] => 1
        [currencycode] => 1
    )

[1810] => Array
    (
        [brand] => Brand 1
        [rate] => 172
        [availability] => 0
        [stopsales] => 0
        [conditions] => 1
        [currencycode] => 1
    )
)

And I want it sorted first by Brand and then by Rate, like this:

Array
(
[3698] => Array
    (
        [brand] => Brand 1
        [rate] => 172
        [availability] => 0
        [stopsales] => 0
        [conditions] => 1
        [currencycode] => 1
    )

[1810] => Array
    (
        [brand] => Brand 1
        [rate] => 198
        [availability] => 0
        [stopsales] => 0
        [conditions] => 1
        [currencycode] => 1
    )

[1805] => Array
    (
        [brand] => Brand 2
        [rate] => 202,5
        [availability] => 0
        [stopsales] => 0
        [conditions] => 1
        [currencycode] => 1
    )

[1801] => Array
    (
        [brand] => Brand 1
        [rate] => 172
        [availability] => 0
        [stopsales] => 0
        [conditions] => 1
        [currencycode] => 1
    )
)

I already got it sorted by "brand" but it's doing it alphabetically, which it's not exactly what I need. The way the "brand" should be sorting is the following:

If I'm in Brand 2's website it should appear first, if I'm in Brand 3 then it should appear first and so on.

Currently I'm using this uasort with the following function:

function sortByBrandName($a, $b) {
//global $hotelBrand;
$brandName = strcmp($a['brand'], $b['brand']);
if($brandName === 0)
{
    return $brandName;
}
return $brandName;
}

And while it does sort the array by Brand, it doesn't do the trick depending on which site I'm currently on

Thanks in advance for your help!

2 Answers 2

1

If you need a needle (or a value) to compare (import) it inside uasort into your uasort, just use the use keyword along with your anonymous function with it. So that in your case, you could use brand name along with the sorting.

Simple Example:

$hotelBrand = 'Brand 3';
uasort($data, function ($a, $b) use ($hotelBrand) {
    $a1 = levenshtein($hotelBrand, $a['brand']);
    $b1 = levenshtein($hotelBrand, $b['brand']);
    if ($a1 === $b1) { // if same name sort by rate
        return $a['rate'] > $b['rate'] ? 1 : -1;
    } else if ($a1 != $b1) {
        return $a1 > $b1 ? 1 : -1;
    }   
    return 0;
});

Sample Output

Sign up to request clarification or add additional context in comments.

2 Comments

This did the job greatly! Thank you very much sir!!
@Agrus sure glad this helped
0
array_multisort(array_column($data, 'brand'),  SORT_ASC,
                array_column($data, 'rate'), SORT_ASC,
                $data);

For Reference: http://php.net/array_multisort

5 Comments

Good one. I like it.. +1
While this does sort the array correctly, it doesn't solve that if I'm on Brand 3's site, it should appear first, not last :(
@Agrus Please check this key 1801 in both you input array and expected array.
@Agrus then use SORT_DESC flag for brand column.
Oh! This was a typo, it's supposed to be 1801 => array ( "brand" => "Brand 3")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.