Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按多个值字段对多维数组php排序_Php_Arrays_Sorting - Fatal编程技术网

按多个值字段对多维数组php排序

按多个值字段对多维数组php排序,php,arrays,sorting,Php,Arrays,Sorting,我有以下数组: Array ( [3698] => Array ( [brand] => Brand 1 [rate] => 198 [availability] => 0 [stopsales] => 0 [conditions] => 1 [currencycode] => 1 ) [1805] => 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
    )
)
我想先按品牌再按价格排序,如下所示:

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
    )
)
我已经把它按“品牌”分类了,但它是按字母顺序排列的,这不完全是我需要的。“品牌”的分类方式如下:

如果我在品牌2的网站,它应该首先出现,如果我在品牌3,那么它应该首先出现,依此类推

目前,我正在使用此uasort并具有以下功能:

function sortByBrandName($a, $b) {
//global $hotelBrand;
$brandName = strcmp($a['brand'], $b['brand']);
if($brandName === 0)
{
    return $brandName;
}
return $brandName;
}
虽然它会按品牌对阵列进行排序,但它不会根据我当前所在的站点进行排序

提前感谢您的帮助

array_multisort(array_column($data, 'brand'),  SORT_ASC,
                array_column($data, 'rate'), SORT_ASC,
                $data);
参考:

如果您需要一个指针(或一个值)将
uasort
中的指针(或值)比较(导入)到您的
uasort
中,只需使用
use
关键字和匿名函数即可。所以,在您的情况下,您可以在排序时使用品牌名称

简单的例子:

$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;
});

好的。我喜欢+1虽然这确实能正确排序数组,但它不能解决如果我在Brand 3的网站上,它应该首先出现,而不是最后出现的问题:(@Agrus请在输入数组和期望数组中检查此键
1801
。@Agrus然后对
Brand
列使用
sort\u DESC
标志。哦!这是一个打字错误,应该是1801=>array(“品牌”=>“品牌3”)这项工作做得非常好!非常感谢您,先生!!@Agrus很高兴这项工作有所帮助