如何为两个特定列排序多维数组

时间:2014-10-01 04:27:45

标签: php arrays sorting multidimensional-array

这是我的数组,它是print_r($json);

的显示

$json数组由$json[] = $display_row;

构建
Array
(
    [0] => Array
        (
            [storeopen] => Closed
            [ShopID] => 1001
            [ShopName] => Allau Shopppp
            [Address] => Coimbatore, Tamil Nadu, India
            [fromTime] => --:--:--
            [toTime] => --:--:--
            [shop_distance] => 1.83
            [deliveryfee] => 10
            [deliverytime] => 10
            [LogoFile] => 961e4f644a88c1d5f9d2a58fabfd7d22.jpg
            [image_path] => imagepath/
        )

    [1] => Array
        (
            [storeopen] => Open
            [ShopID] => 1002
            [ShopName] => Pizza Hut
            [Address] => Coimbatore, Tamil Nadu, India
            [fromTime] => 7:00 am
            [toTime] => 6:00 pm
            [shop_distance] => 1.83
            [deliveryfee] => 7
            [deliverytime] => 25
            [LogoFile] => 2d40078c278b1e41fc074ef35e6f2240.jpg
            [image_path] => imagepath/
        )

    [2] => Array
        (
            [storeopen] => Open
            [ShopID] => 1009
            [ShopName] => shop1
            [Address] => Saibaba Colony, Coimbatore, Tamil Nadu, India
            [fromTime] => 4:45 am
            [toTime] => 5:00 pm
            [shop_distance] => 3.32
            [deliveryfee] => 36
            [deliverytime] => 70
            [LogoFile] => a123ea694d8970647213ce4d316c7d2a.jpg
            [image_path] => imagepath/
        )

)

我想通过Multisort

对其进行排序
$sorted = array_orderby($json, $display_row['storeopen'], SORT_DESC, $display_row['shop_distance'], SORT_ASC);

我该怎么做?

1 个答案:

答案 0 :(得分:1)

你必须构建"列数组"进入array_multisort();您可以查看PHP文档here或查看this eval.in;这是您问题中最相关的部分:

foreach($json as $key => $value) {
    $storeOpen[$key]    = $value['storeopen'];
    $shopDistance[$key] = $value['shope_distance'];
}

array_multisort($storeOpen, SORT_DESC, $shopDistance, SORT_ASC, $json);