A
A
AdrianBlair2017-03-16 18:45:10
PHP
AdrianBlair, 2017-03-16 18:45:10

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"
  }
]

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2017-03-16
@AdrianBlair

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

or a bit of shitty code:
$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 question

Ask a Question

731 491 924 answers to any question