Answer the question
In order to leave comments, you need to log in
How to find the maximum number in a two-dimensional array?
Hello!
We need code that calculates the maximum value of the y parameter in the data array, where the value of the x parameter is 2.
Array example (json_encode):
[
{
"x": "1",
"y": "1"
},
{
"x": "1",
"y": "2"
},
{
"x": "1",
"y": "3"
},
{
"x": "1",
"y": "4"
},
{
"x": "1",
"y": "5"
},
{
"x": "2",
"y": "1"
},
{
"x": "2",
"y": "2"
},
{
"x": "2",
"y": "3"
},
{
"x": "2",
"y": "4"
},
{
"x": "2",
"y": "5"
}
]
Answer the question
In order to leave comments, you need to log in
Why are you programming if you don't even want to look up the information yourself or even try a little?
$data = [...]; // ваши данные
$data = array_filter($data, function($item) {
return $item['x'] == 2;
});
$data = array_map(function($item) {
return $item['y'];
}, $data);
$max = max($data);
$data = [...]; // ваши данные
$max = max(array_map(
function($item) {
return $item['y'];
},
array_filter($data, function($item) {
return $item['x'] == 2;
})
));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question