0

提前感谢大家的帮助。

我有以下多维数组:

[list] => Array (
    [0] => Array (
        [retailer] => ABC Store
        [locations] => Array (
            [0] => Array (
                [distance] => 2.86
            )
            [1] => Array (
                [distance] = 5.50
            )
        )
    )

    [1] => Array (
        [retailer] => XYZ Store
        [locations] => Array (
            [0] => Array (
                [distance] => 1.25
            )
            [1] => Array (
                [distance] = 7.50
            )
        )
    )
)

我想根据位置数组中的最近距离对零售商进行排序。所以新的Array会列XYZ Store在前面ABC Store

我尝试过使用array_multisort,但是我在将主数组与嵌套了几层的键进行排序时遇到问题。

4

1 回答 1

0
$list = array(
    array(
        "retailer" => "ABC Store",
        "locations" => array(
            array(
                "distance" => 2.86
            ) ,
            array(
                "distance" => 5.50
            )
        )
    ) ,
    array(
        "retailer" => "XYZ Store",
        "locations" => array(
            array(
                "distance" => 1.25
            ) ,
            array(
                "distance" => 7.50
            )
        )
    )
);

foreach($list as $info) {
    $storeinfo[$info["retailer"]] = $info;
    foreach($info["locations"] as $location) {
        $distances[] = $location["distance"];
    }
    $stores[$info["retailer"]] = min($distances);
}

natsort($stores);

foreach($stores as $store => $distance) {
    $newlist[] = $storeinfo[$store];
}

echo "<pre>";
print_r($newlist);
echo "</pre>";
于 2013-02-04T18:10:16.110 回答