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.
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