Thanks in advance for everyone's help.
I have the following multidimensional array:
[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
)
)
)
)
I would like to sort the retailers based on the closest distance within the locations array. So the new Array
would list XYZ Store
ahead of ABC Store
.
I've tried using array_multisort
, but I'm having issues sorting the main array against a key that's nested several layers down.
$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>";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.