get the maximum price of the associative array in php

443 views Asked by At

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.

4

There are 4 answers

0
jspit On

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

2
Anisur Rahman On

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

0
MUKUL HOSEN On

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
0
erfaneh sahraii On

enter image description here

Because we are sure that the data we have is not empty, we can first assume that the first cell of the given data is the maximum, so we put a variable called maxKey $ (which you put) and set it to zero.

Now in a quick h, we check if each item is worth more than the price at home maxKey $ or not, if so, we will update the value maxKey $.

Finally it is enough to return the value of data [$ maxKey] ['name'] $ (which is one of the problems with your current code is that you return the maximum value in immediate HR while there may be some more) There is another problem with you: the value of $ key is not defined (in line 31) and also in For HH you have to compare the value of the item, which now compares the item itself, which is an associative array.