获取php中关联数组的最高价格
我有以下数组,
$data = [
[
'name' => 'Electric Khodro',
'price' => 12912
],
[
'name' => 'Iran Khodro',
'price' => 15218
],
[
'name' => 'Iran arghaam',
'price' => 8853
]
];
我想从数组中获取最高价格名车的密钥,这是上面数组中的快乐。 这里有两个提示:
- 如果 $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:
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 array_column 从 ' 获取一维数组价格'。然后 php 有函数 max() 来获取最大值。
函数的定义只有在也使用参数时才有意义。如果没有参数,您就必须使用全局变量,但 PHP 中没有人不这样做。
如果数组 $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.
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.
The function returns NULL if the array $data is empty or the name does not exist as a column.
Try self on 3v4l.org
根据您的要求,如果
getHighestPrice()
函数应该没有参数,那么您必须从全局范围获取$data
。As your requirement, If the
getHighestPrice()
function should have no parameters then you have to get the$data
from global scope.我认为你想要最高值输出的键
:
I think you want the key of the highest value
OUTPUT:
在此处输入图片描述
因为我们确定我们拥有的数据不为空,所以我们可以先假设给定数据的第一个单元格是最大值,因此我们放置一个名为 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.