2

I have following array:

$array = [
    [2000, 401],
    [2000, 317],
    [2000, 301],
    [2002, 285],
    [2002, 277],
    [2002, 271],
    [2001, 208],
    [2001, 263],
    [2002, 285],
    [2001, 262],
    [2000, 258]
]

Then I wrote following code to group its values:

$result06 = array();
foreach ($chart06 as $el) {
    if (!array_key_exists($el[0], $result06)) {
        $result06[$el[0]] = array();
    }
    $result06[$el[0]][] = $el[1];
}

With that we receive following result:

Array
(
[2000] => Array
    (
        [0] => 401
        [1] => 317
        [2] => 301
        [3] => 258
    )

[2002] => Array
    (
        [0] => 285
        [1] => 277
        [2] => 271
    )

[2001] => Array
    (
        [0] => 208
        [1] => 263
        [2] => 262
    )
)

Now how can I determine the max value of each subarray and transform it in a "flat" array just like the following?

Array
(
[2000] => 401
[2002] => 285
[2001] => 263
)

I tried this:

foreach ($result06 as $value){
    $resultMax[] = max($value);
}

but with that the result is:

Array
(
[0] => 401
[1] => 285
[2] => 263
)

But I really need the original keys being the same.

4 Answers 4

1

You can get the key in the foreach loop like this:

foreach ($result06 as $key => $value){
    $resultMax[$key] = max($value);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Only insert the max in the first place:

foreach ($chart06 as $el) {
    if (!array_key_exists($el[0], $result06)) {
        $result06[$el[0]] = $el1;
    } else {
        $result06[$el[0]] = max($result06[$el[0]], $el[1]);
    }
}

Comments

0

Skip the intermediate step of creating the grouping array:

$result06 = array();

foreach ($chart06 as $el) {
    if (!isset($result06[$el[0]])) {
        $result06[$el[0]] = 0;
    }
    $result06[$el[0]] += el[1];
}

Comments

0

I agree with AbraCadaver, you can definitely cut out the middle step of grouping your values by the identifier.

You just need to iterate the original input array and determine:

  1. if each subset contains the first occurrence of the "identifier" (first value in row) OR
  2. if it is not the first occurrence, check if it is larger than the earlier stored value.

Code: (Demo)

$array = [
    [2000, 401],
    [2000, 317],
    [2000, 301],
    [2002, 285],
    [2002, 277],
    [2002, 271],
    [2001, 208],
    [2001, 263],
    [2002, 285],
    [2001, 262],
    [2000, 258]
];

foreach ($array as $row) {
    $id = $row[0];                                         // just to improve readability
    if (!isset($result[$id]) || $result[$id] < $row[1]) {  // first occurrence or less than current
        $result[$id] = $row[1];                            // store the current value
    }
}

var_export($result);

Output:

array (
  2000 => 401,
  2002 => 285,
  2001 => 263,
)

Or if you want to keep the full rows, you can adjust it like so: (Demo)

$result = [];
foreach ($array as $row) {
    $id = $row[0];                                            // just to improve readability
    if (!isset($result[$id]) || $result[$id][1] < $row[1]) {  // first occurrence or less than current
        $result[$id] = $row;                                  // store the current value
    }
}
var_export(array_values($result));

Output:

array (
  0 => 
  array (
    0 => 2000,
    1 => 401,
  ),
  1 => 
  array (
    0 => 2002,
    1 => 285,
  ),
  2 => 
  array (
    0 => 2001,
    1 => 263,
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.