Home > Software engineering >  get the maximum price of the associative array in php
get the maximum price of the associative array in php

Time:03-03

I have the following array

$data = [
[
    'name' => 'Electric Khodro',
    'price' => 12912
],
[
    'name' => 'Iran Khodro',
    'price' => 15218
],
[
    'name' => 'Iran arghaam',
    'price' => 8853
]
];

I want to get key of the maximum price name car from the array that is joy from the above array. There are two tips in question:

  1. If the value of the $ data variable was empty, the function must return the null value.

۲. The getHighestPrice function should have no parameters. The general view of the codes is as follows:

<?php

$data = [
    [
        'name' => 'Electric Khodro',
        'price' => 12912
    ],
    [
        'name' => 'Iran Khodro',
        'price' => 15218
    ],
    [
        'name' => 'Iran arghaam',
        'price' => 8853
    ]
    ,
    // ...
];

function getHighestPrice()
{
    // TODO: Implement
}

Thank you for helping in advance.

CodePudding user response:

You can use array_column to get a one dimensional array from 'price'. php then has the function max() for the maximum.

$maxPrice = max(array_column($data,'price'));

The definition of a function only makes sense if it also uses parameters. Without parameters, you would have to work with global variables, but nobody in PHP doesn't do that.

function getHighestPrice($data,$name){
  $prices = array_column($data,$name);
  return $prices == [] ? NULL : max($prices);
}

$maxPrice = getHighestPrice($data,'price');

The function returns NULL if the array $data is empty or the name does not exist as a column.

Try self on 3v4l.org

CodePudding user response:

As your requirement, If the getHighestPrice() function should have no parameters then you have to get the $data from global scope.

<?php
$data = [
    [
        'name' => 'Electric Khodro',
        'price' => 12912
    ],
    [
        'name' => 'Iran Khodro',
        'price' => 15218
    ],
    [
        'name' => 'Iran arghaam',
        'price' => 8853
    ]
];

function getHighestPrice()
{
    $data = $GLOBALS['data'] ?? null;// Get $data variable

    if(empty($data)){
        return null;// If empty then return null
    }

    // Sorting
    usort($data, function($a, $b) {
        return $a['price'] < $b['price'];
    });

    // Return the maximum price 
    return $data[0]['price'];

    // Return the car name of maximum price 
    /*
    return $data[0]['name'];
    */
}

echo getHighestPrice();

Output: 15218

CodePudding user response:

I think you want the key of the highest value

$data = [
    [
        'name' => 'Electric Khodro',
        'price' => 12912
    ],
    [
        'name' => 'Iran Khodro',
        'price' => 15218
    ],
    [
        'name' => 'Iran arghaam',
        'price' => 8853
    ]
];

echo(getHighestPrice($data));

function getHighestPrice($array = [])
{
    $max = null;
    $result = null;
    foreach ($array as $key => $value) {
        if ($max === null || $value['price'] > $max) {
            $result = $key;
            $max = $value['price'];
        }
    }

    return $result;
}

OUTPUT:

1
  • Related