如何从下面的数组中获得最大距离?当我尝试print_r($ data)时,我得到以下输出:
Array
(
[0] => Array
(
[distance] => 1.7 km
[time] => 3 mins
[distance_value] => 1720
[time_value] => 192
)
[1] => Array
(
[distance] => 4.2 km
[time] => 10 mins
[distance_value] => 4207
[time_value] => 587
)
)
我想回声4.2 km,因为它是我阵列中的最大距离。
foreach ($delivery as $key => $value) {
if($key==0) {
$mysource = $pickup;
} else {
$mysource = $delivery[$key-1];
}
$data[$key] = $this->GetDrivingDistance($mysource,$value);
if(!empty($data[$key])) {
$dist += max($data[$key]['distance']);
}
}
echo $dist; exit();
print_r($data); exit();
答案 0 :(得分:6)
您可以使用内置功能:
echo max(array_map('floatval',array_column($array, "distance")))." km";
<强>解释强>
array_column
- 将您的数组转换为单维array_map
- 对字符串应用浮点运算。这很重要,因为您的字符串比较错误地比较了两个数字。当应用浮动函数时,您的km
将被移除,因此稍后会附加。字符串示例:
11.7小于4.2,因为它比较第一个字符并按1,2,3 ...
排列<强>输出:强>
4.2 km
注意:强>
如果所有值都在km
,这是合适的,如果它在其他单位中,您也需要一个解决方法!
答案 1 :(得分:0)
public void CopyDataBaseFromAsset() throws IOException{
InputStream in = ctx.getAssets().open(DATABASE_NAME);
Log.e("sample", "Starting copying" );
String outputFileName = DATABASE_PATH+DATABASE_NAME;
File databaseFile = new File(DATABASE_PATH);
// check if databases folder exists, if not create one and its subfolders
if (!databaseFile.exists()){
databaseFile.mkdir();
}
OutputStream out = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer))>0){
out.write(buffer,0,length);
}
Log.e("sample", "Completed" );
//db_delete();
out.flush();
out.close();
in.close();
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
if (newVersion > oldVersion) {
db.close();
boolean b = ctx.deleteDatabase(DATABASE_NAME);
if(b){
try{
CopyDataBaseFromAsset();
}
catch (IOException e){
throw new Error("Error copying database");
}
}
}
}
答案 2 :(得分:0)
您可以先按距离对数组进行排序,然后获取所需数据:
$myArray =
array (
array
(
'distance' => '1.7 km',
'time' => '3 mins',
'distance_value' => 1720,
'time_value' => 192,
),
array
(
'distance' => '4.2 km',
'time' => '10 mins',
'distance_value' => 4207,
'time_value' => 587,
)
);
usort($myArray,function($a, $b) {
return $b['distance_value'] - $a['distance_value'];
});
$maxDistance = $myArray[0];
echo $maxDistance['distance'];
// 4.2 km
答案 3 :(得分:0)
我从下面的代码中获得解决方案..
foreach ($delivery as $key => $value) {
if($key==0){
$mysource = $pickup;
}else{
$mysource = $delivery[$key-1];
}
$data[$key] = $this->GetDrivingDistance($mysource,$value);
if(!empty($data[$key])){
$max = '-9999999 km'; //will hold max val
$found_item = null; //will hold item with max val;
foreach($data as $k=>$v)
{
if($v['distance']>$max)
{
$max = $v['distance'];
$found_item = $v;
}
}
}
}
$dist = $max;
答案 4 :(得分:0)
Thamilan's answer是最好的,但是当您使用CakePHP时,如果Hash::extract()
无法使用,您也可以使用array_column()
( ie < / em>您使用旧版本的PHP)或者由于Hash
实用程序的路径语法,您需要使用具有更深层关联的数组: -
echo max(array_map('floatval', Hash::extract($array, 'distance'))) . ' km';
此处Hash::extract()
的工作方式与array_column()
类似,并将您的数组转换为单维数组。
答案 5 :(得分:0)
正确这个问题用CakePHP标记我虽然我使用CakePHP Collection class来提供我的解决方案:
use Cake\Collection\Collection;
(new Collection($data))->sortBy('distance_value')->first()['distance'];
请注意,CakePHP有一个实用程序函数collection
可以缩短它:
collection($data)->sortBy('distance_value')->first()['distance']