简体   繁体   中英

Max value on 2d array with condition?

I have an array with these values:

Array
(
    [0] => Array
    (
        [0] => John
        [1] => 2012-03-29
        [2] => 1
    )
    [1] => Array
    (
        [0] => Doe
        [1] => 2012-03-30
        [2] => 1
    )
    [2] => Array
    (
        [0] => John
        [1] => 2012-03-31
        [2] => 2
    )
    [3] => Array
    (
        [0] => Doe
        [1] => 2012-03-31
        [2] => 5
    )
    [4] => Array
    (
        [0] => John
        [1] => 2012-04-02
        [2] => 5
    )
    [5] => Array
    (
        [0] => John
        [1] => 2012-04-02
        [2] => 21
    )
)

I'm trying to get the maximum value of array[][2] for each date in array[][1] .

For example, in the array

Array[2][1] = 2012-03-31 | Array[2][2] = 2
Array[3][1] = 2012-03-31 | Array[3][2] = 5

Out of those two same days I want to keep Array[3] and discard Array[2] .

I tried a foreach function:

foreach ($days as $k) {
    $max_vals[] = array($k[0], $k[1], max($k[2]));
}

But found that is not working with the days. How can I sort this array keeping the highest value for each day, and discard the rest?

Here's one way to solve this :

  • Loop over array with foreach

  • In foreach loop group new array by name, date, number

  • Compare with previous values if exists and overwrite if number is higher

Hope this helps.

Try this:

$bigArray=array(
array('John','2012-03-29',1),
array('Doe','2012-03-30',1),
array('John','2012-03-31',2),
array('Doe','2012-03-31',5),
array('John','2012-04-02',5),
array('John','2012-04-02',21),
array('John','2012-03-07',21)   
);
$output=array();
foreach($bigArray as $element){
if($element[1]>$output[$element[2]]){
$output[$element[2]]=$element[1];
}}

echo '<pre>';
print_r($output); 

This outputs:

Array
(
    [1] => 2012-03-30
    [2] => 2012-03-31
    [5] => 2012-04-02
    [21] => 2012-04-02
)

or to get the opposite:

   $bigArray=array(
    array('John','2012-03-29',1),
    array('Doe','2012-03-30',1),
    array('John','2012-03-31',2),
    array('Doe','2012-03-31',5),
    array('John','2012-04-02',5),
    array('John','2012-04-02',21),
    array('John','2012-03-07',20)   
    );
$output=array();
    foreach($bigArray as $element){
    if($element[2]>$output[$element[1]]){
    $output[$element[1]]=$element[2];
    }}
echo '<pre>';
print_r($output); 

This prints:

Array
(
    [2012-03-29] => 1
    [2012-03-30] => 1
    [2012-03-31] => 5
    [2012-04-02] => 21
    [2012-03-07] => 20
)

The following code will iterate across each value in $data, either added newly encountered dates to the $cache or checking if it is larger.

$cache = array();
// Note: $data is the array you provided above, replace with your variable name.
foreach ($data as $k => $v) {
  if (isset($cache[$v[1]])) {
    if ($cache[$v[1]] < $v[2]) {
      $cache[$v[1]] = $v[2];
    }
  } else {
    $cache[$v[1]] = $v[2];
  }
}

When finished, $cache will be an array with the values being dates and the keys the max value of array[][2] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM