我有一个如下所示的数组列表,
Array(
[1] => Array
(
[id] => 18
[name] => mac
[country_code] => +91
[phone] => 1325647890
[distance] => 15 m
[address] => sdfghjk
[city] => Place_1
[state] => Kerala
[postal_code] => 682030
)
[2] => Array
(
[id] => 18
[name] => Paul
[country_code] => +91
[phone] => 1325647890
[distance] => 32.1 m
[address] => sdfghjk
[city] => Place_1
[state] => Kerala
[postal_code] => 686610
)
[3] => Array
(
[id] => 18
[name] => John
[country_code] => +91
[phone] => 1325647890
[distance] => 3 m
[address] => sdfghjk
[city] => Place_1
[state] => Kerala
[postal_code] => 682030
)
)
我需要使用minimum value of [distance]
即,所需的输出将是,
[3] => Array
(
[id] => 40
[name] => John
[country_code] => +91
[phone] => 1234567809
[distance] => 3 m
[address] => bddf
[city] => Place_3
[state] => Kerala
[postal_code] => 682030
)
答案 0 :(得分:2)
您可以尝试这样:
您需要将所有距离转换为相同的单位,然后才能找到最小距离。
$serverName = "domserverqa";
$databases = (Get-AzureRmSqlDatabase -ResourceGroupName $resourceGroup -ServerName $serverName).DatabaseName
$Backupdatatime = "Standard time of the backup for all databases" # E.g. "2018-01-09-07:00"
foreach($database in $databases)
{
$primarykey = $STORAGE_ACCOUNT_BACKUP_KEY; #strdatabasebackup
# Assuming strdatabase as a standard name given to the storage accounts and other databases follow the naming conventions.
$StorageUri = "https://" + $database + "backup" + ".blob.core.windows.net/" + $database + "backupblob" + "/" + "(" + $Backupdatatime + ")" + $databases + ".bacpac"
#Further actions.....
}
答案 1 :(得分:0)
一个简单的array_reduce会为你处理这个问题,你需要将距离字符串转换为相同的基数(km - > m),转换为数字也可以更好地进行比较:
$source = [
[
'id' => 18,
'name' => 'Mac',
'distance' => '15 m'
// ...
],
[
'id' => 27,
'name' => 'paul',
'distance' => '32.1 km'
// ...
],
[
'id' => 40,
'name' => 'John',
'distance' => '3 m'
// ...
]
];
function convertDistanceToMeter($from) {
list($value, $unit) = explode(' ', $from);
switch($unit) {
case 'm': return $value * 1;
case 'km': return $value * 1000;
default: throw new Exception("Unit '$unit' not supportet.");
}
}
$result = array_reduce($source, function($carry, $item) {
if(!$carry) return $item;
$itemDst = convertDistanceToMeter($item["distance"]);
$carryDst = convertDistanceToMeter($carry["distance"]);
return $itemDst < $carryDst ? $item : $carry;
});
print_r($result);
输出符合预期:
Array
(
[id] => 40
[name] => John
[distance] => 3 m
// ...
)
答案 2 :(得分:0)