0

I have an array like this:

$flight = array (
    array (
        "home" => "AMS",
        "away" => "LHR",    
        "price" => "270"
    ),
    array (
        "home" => "AMS",
        "away" => "LGW",    
        "price" => "216"
    ),   
    array (
        "home" => "EIN",
        "away" => "LHR",    
        "price" => "427"
    ) 
);

I want the values from the cheapest flight. The result should be: AMS LGW 216.

When I loop trough the array I can get the lowest price result but not the other values from the array home and away.

foreach ($flight as $value) { 
    echo $value["home"].' '.$value["away"].' '.$value["price"].'<br>';
    
    $lowest = array_column($flight, "price");
    $min = min($lowest);
}

echo $min;

The result now is only 216 But the result I want AMS LGW 216

How can I create this?

breakslow_
  • 51
  • 1
  • 9
  • Related: https://stackoverflow.com/q/44660139/2943403 and https://stackoverflow.com/q/57013532/2943403 and https://stackoverflow.com/q/44098099/2943403 and https://stackoverflow.com/q/44713151/2943403 and https://stackoverflow.com/q/50986556/2943403 and https://stackoverflow.com/q/37662494/2943403 – mickmackusa Jul 31 '22 at 22:56
  • What is the desired result when two rows in the array have the exact same price? You are not the first developer in the world to require this functionality. – mickmackusa Jul 31 '22 at 23:02
  • If your data comes from a database, use SQL, not PHP. https://stackoverflow.com/q/54328379/2943403 – mickmackusa Jul 31 '22 at 23:07
  • Also related: https://stackoverflow.com/q/30315929/2943403 – mickmackusa Aug 01 '22 at 01:06

5 Answers5

0

Store the entire item not just the price.

$min = ["price" => 1e10];
foreach ($flight as $value) {
    echo $value["home"] . ' ' . $value["away"] . ' ' . $value["price"] . '<br>';

    if ($value['price'] < $min['price']) {
        $min = $value;
    }
}
print_r($min);
IT goldman
  • 14,885
  • 2
  • 14
  • 28
0

One option is to remember what the values were at the lowest price and just iterate over all of them:

$min = null;

foreach ($flight as $value) { 
    
    if ($min === null || $value['price'] < $min) {
        $minValue = $value;
        $min = $value['price'];
    }
}

echo $minValue["home"].' '.$minValue["away"].' '.$minValue["price"];
ArSeN
  • 5,133
  • 3
  • 19
  • 26
0

Sort the array by field (ASC order) and you'll find the lowest element in the head of your array:

usort($flights, fn($a, $b) => $a['price'] <=> $b['price'])

print_r($flights[0]);
user1597430
  • 1,138
  • 1
  • 7
  • 14
0

You can use array_keys and with your code.

Here is the code:

    $flight = array(
        array(
            "home" => "AMS",
            "away" => "LHR",
            "price" => "270"
        ),
        array(
            "home" => "AMS",
            "away" => "LGW",
            "price" => "216"
        ),
        array(
            "home" => "EIN",
            "away" => "LHR",
            "price" => "427"
        )
    );

    $lowest = array_column($flight, "price");
    $lowset_array = array_keys($lowest, min($lowest));
    print_r($flight[reset($lowset_array)]);

    //OR
    //print_r($flight[$lowset_array[0]]);

And here is the output:

Array
(
    [home] => AMS
    [away] => LGW
    [price] => 216
)
Murad Ali
  • 347
  • 2
  • 15
0

Before providing solutions: your implementation it's finding the lowest price several times (the number of flights). You can get the cheapest price with:

$lowest = array_column($flight, 'price');
echo min($lowest);

You may use two variables to save the lowest price and the associated flight:

function getCheapestFlight(array $flights): array
{
    $cheapestFlight = null;
    $lowest = PHP_INT_MAX;
    foreach ($flights as $flight) {
        $price = $flight['price'];
        if ($price < $lowest) {
            $lowest = $price;
            $cheapestFlight = $flight;
        }
    }
    return $cheapestFlight;
}

Or use only one variable for the cheapest flight:

function getCheapestFlight(array $flights): array
{
    $cheapestFlight = reset($flights);
    foreach ($flights as $flight) {
        $price = $flight['price'];
        if ($price < $cheapestFlight['price']) {
            $cheapestFlight = $flight;
        }
    }
    return $cheapestFlight;
}

If you prefer functional programming:

function getCheapestFlight(array $flights): array
{
    return array_reduce(
        $flights,
        function ($cheapestFlight, $flight) {
            return $flight['price'] < $cheapestFlight['price']
                ? $flight
                : $cheapestFlight;
        },
        reset($flights)
    );
}
Pedro Amaral Couto
  • 2,056
  • 1
  • 13
  • 15
  • To avoid wasting your precious time and to ensure that your carefully crafted answer isn't lost, please look for duplicates before answering. If you can find an earlier page asking for the same resolution, post your wisdom there and close the new question. Thank you. – mickmackusa Aug 01 '22 at 01:08