2

How can I get the maximum distance from the array below? I am getting the following output when i try to 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
        )

)

I want to echo 4.2 km because it is the max distance in my array.

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();
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
parcel pik
  • 145
  • 1
  • 8

6 Answers6

6

You can make use of built-in functions:

echo max(array_map('floatval',array_column($array, "distance")))." km";

Explanation:

  • array_column - converts your array to single dimensional
  • array_map - Apply float operation to the string. This is important since your string comparison compares two numbers wrongly. When float function is applied your km will be removed, so append later on.

Example for string:

11.7 is less than 4.2, because it compares first character and arranges by 1,2,3...


Output:

4.2 km

Note: This is suitable if all values are in km,if it is in other units, you need a workaround too!

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0
<?php

$array = 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,
        )
);

$distance = [];
$i = 0;
foreach ($array as $distance_data) {
    $distance[] = (float)$distance_data["distance"];
    $i++;
}
$max_distnace = max($distance);
var_dump($max_distnace);
M A SIDDIQUI
  • 2,028
  • 1
  • 19
  • 24
0

You can first sort the array by distance and get the required one:

$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
Ulugov
  • 347
  • 4
  • 9
  • 1
    if you reverse the usort comparison, you don't need array_reverse `usort($data, function ($a, $b){ return $b['distance_value'] - $a['distance_value'];});` and `echo $data[0]['distance'];`. Here is [a demo](https://eval.in/712928) – bansi Jan 10 '17 at 05:19
  • Are you sure you tested your code with dynamic values? – Thamilhan Jan 10 '17 at 05:25
  • `distance` and `distance_value` keep the same value. Using `distance_value` to compare more stable way I think. This code is tested. – Ulugov Jan 10 '17 at 05:30
0

I get solution from below code..

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;
parcel pik
  • 145
  • 1
  • 8
0

Thamilan's answer is the best, but as you're using CakePHP you can also use Hash::extract() if array_column() isn't available to you (i.e. you're using an older version of PHP) or if you need to use an array with deeper associations thanks to the Hash utility's path syntax:-

echo max(array_map('floatval', Hash::extract($array, 'distance'))) . ' km';

Here Hash::extract() is working like array_column() and converting your array to a single dimensional array.

Community
  • 1
  • 1
drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
0

Sine this questions is tagged with CakePHP I though I'd give my solution using CakePHP Collection class:

use Cake\Collection\Collection;
(new Collection($data))->sortBy('distance_value')->first()['distance'];

Note that CakePHP has a utility function collection that can make this shorter:

collection($data)->sortBy('distance_value')->first()['distance']
ankr
  • 647
  • 6
  • 15