-2

I have an array list like below,

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
    )
)

I need to pick single array from the above list with minimum value of [distance]

Ie, required output will be,

[3] => Array
(
    [id] => 40
    [name] => John
    [country_code] => +91
    [phone] => 1234567809
    [distance] => 3 m
    [address] => bddf
    [city] => Place_3
    [state] => Kerala
    [postal_code] => 682030
)
Ravi Sachaniya
  • 1,641
  • 18
  • 20
Achu S
  • 137
  • 9

3 Answers3

2

You can try like this :

You need to simply convert all the distances to the same units then you can find the minimum distance.

$min = 0;
$output = false;
foreach ($arr as $key => $plas) {
    list($dist, $units) = explode(' ', $plas['distance']);

    switch ($units) {
        case 'km':
            $base_dist = ($dist * 1000);
            break;
        case 'm':
            $base_dist = ($dist * 1);
            break;
        default:
            throw new Exception("Unit is invalid...!");
            break;
    }
    if ($base_dist < $min || $min == 0) {
        $min = $base_dist;
        $output = array($key => $plas);
    }
}
print_r($output);
Ravi Sachaniya
  • 1,641
  • 18
  • 20
0

A simple array_reduce will handle this for you and you need to convert the distance-string to the same base (km -> m), converting to number is also better for comparison:

$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);

Output is as expected:

Array
(
    [id] => 40
    [name] => John
    [distance] => 3 m
    // ...
)
Steven
  • 381
  • 2
  • 10