获取php中关联数组的最高价格

发布于 2025-01-11 11:19:30 字数 756 浏览 0 评论 0原文

我有以下数组,

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

我想从数组中获取最高价格名车的密钥,这是上面数组中的快乐。 这里有两个提示:

  1. 如果 $data 变量的值为空,则该函数必须返回 null 值。

2. getHighestPrice 函数不应有参数。 代码的总体视图如下:

<?php

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

function getHighestPrice()
{
    // TODO: Implement
}

感谢您提前提供帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久隐师 2025-01-18 11:19:30

您可以使用 array_column 从 ' 获取一维数组价格'。然后 php 有函数 max() 来获取最大值。

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

函数的定义只有在也使用参数时才有意义。如果没有参数,您就必须使用全局变量,但 PHP 中没有人不这样做。

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

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

如果数组 $data 为空或名称不作为列存在,则该函数返回 NULL。

自行尝试 3v4l.org

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

深居我梦 2025-01-18 11:19:30

根据您的要求,如果 getHighestPrice() 函数应该没有参数,那么您必须从全局范围获取 $data

<?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();

输出:15218

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

七月上 2025-01-18 11:19:30

我认为你想要最高值输出的键

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

1

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
驱逐舰岛风号 2025-01-18 11:19:30

在此处输入图片描述

因为我们确定我们拥有的数据不为空,所以我们可以先假设给定数据的第一个单元格是最大值,因此我们放置一个名为 maxKey $ 的变量(您放置的)并将其设置为零。

现在,我们快速检查每件物品的价值是否超过家里的价格 maxKey $ ,如果是,我们将更新值 maxKey $ 。

最后返回数据的值 [$ maxKey] ['name'] $ 就足够了(这是您当前代码的问题之一是您返回即时 HR 中的最大值,而可能还有更多)
您还有另一个问题: $ key 的值未定义(第 31 行),并且在 For HH 中您必须比较项目的值,现在比较项目本身,这是一个关联数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文