从关联数组foreach中获取最高价值

时间:2015-07-25 23:10:11

标签: php arrays foreach max associative-array

我正在实施此处显示的第一个答案:Finding Highest Value in Associative Array

但是发生的事情是它将它找到的第一个值指定为“max”,然后当它找到下一个最高值时,它也会为该值指定“max”。

当然我的目标是只将“max”分配给最高值。

问题必须是我提出价值观的方式。

这是数组(我在PER WEEK的基础上为'sum'索引分配“max”):

Array
(
    [0] => stdClass Object
        (
            [userid] => 1
            [week] => 1
            [sum] => 28
            [user] => al
        )

[1] => stdClass Object
    (
        [userid] => 1
        [week] => 2
        [sum] => 33
        [user] => al
    )

[2] => stdClass Object
    (
        [userid] => 1
        [week] => 3
        [sum] => 29
        [user] => al
    )
  ...

[17] => stdClass Object
    (
        [userid] => 26
        [week] => 1
        [sum] => 14
        [user] => bob
    )

[18] => stdClass Object
    (
        [userid] => 26
        [week] => 2
        [sum] => 23
        [user] => bob
    )

[19] => stdClass Object
    (
        [userid] => 26
        [week] => 3
        [sum] => 35
        [user] => bob
    )
  ...
[34] => stdClass Object
    (
        [userid] => 27
        [week] => 1
        [sum] => 36
        [user] => chuck
    )

[35] => stdClass Object
    (
        [userid] => 27
        [week] => 2
        [sum] => 23
        [user] => chuck
    )

[36] => stdClass Object
    (
        [userid] => 27
        [week] => 3
        [sum] => 29
        [user] => chuck
    )

但我通过几个foreach循环传递数据:

<?php
$resultsByUser = array();
$temp = array();
foreach($results as $result) {
    $resultsByUser[$result->user][$result->week] = $result->sum;
}
foreach($resultsByUser as $user => $resultsByWeek) {
    print "<tr><td>$user</td>";
    ksort($resultsByWeek);

    foreach($resultsByWeek as $week => $sum) {
        $temp[$week] = max(
                           isset($temp[$week]) ? $temp[$week] : $sum,
                           $sum);                           

        if($week == $week) {    
            if($temp[$week] == $sum) {  ?>                   
                <td><?php echo $sum; ?><span><i class='fa fa-trophy'></i></span></td>
                <?php } else { ?>
            <td><?php echo $sum; ?></td>
            <?php }
        }
    }
} ?>

在分配奖杯图标之前,如何强制$ temp [week]又称“max”函数循环遍历每周值的整个关联数组?

编辑: 在评论之后,我已经将最后一对节改为下面的节,但现在我没有将奖杯分配给任何值。我错过了什么?

foreach($resultsByWeek as $week => $sum) {
        $temp[$week] = max(
          isset($temp[$week]) ? $temp[$week] : $sum,
                 $sum); ?>                      

        <td><?php echo $sum; }}?>

    <?php if($sum == $temp[$week]) {    ?>  <span><i class='fa fa-trophy'></i></span></td>
                         <?php } ?>

1 个答案:

答案 0 :(得分:2)

你在另一篇文章中收到的答案:

$temp = array();
foreach ($data as $item) {
    foreach ($item as $key => $value) {
        $temp[$key] = max(
                isset($temp[$key]) ? $temp[$key] : $value,
                $value);
    }
}

您浏览所有对象数组,在浏览所有数据元素时更新最大值。 这意味着当您到达该循环的结尾时,您将只有最大值(100%确定性)

如果您想要的是在最大值旁边打印奖杯图标,您需要事先知道该元素是什么。

您有两种方法可以做到这一点:

<强> 1。使用两个循环

使用第一个循环,无需写入,只是为了找到最大值。 然后使用第二个循环,写入,写入对象数据。并且,如果在第一个循环中收集的最大值等于对象数据,则绘制奖杯图标。

类似的东西:

$temp = array();
foreach ($data as $item) {
    foreach ($item as $key => $value) {
        $temp[$key] = max(
                isset($temp[$key]) ? $temp[$key] : $value,
                $value);
    }
}

foreach ($data as $item) {
    foreach ($item as $key => $value) {
        echo $user_data; 
        if ($user_data_sum == $temp[$key])
            echo "<trophy image>"; 
    }
}

<强> 2。保存对象,它是最大值(或奖杯)

如果可以编辑对象,请在对象上保存最大值。这将缩短处理时间,因为您只需要一个循环而不是上面的两个循环。

类似的东西:

[0] => stdClass Object
    (
        [userid] => 1
        [week] => 1
        [sum] => 28
        [user] => al
        [is_max] => false
    )
[1] => stdClass Object
    (
        [userid] => 1
        [week] => 2
        [sum] => 33
        [user] => al
        [is_max] => true
    )

在循环中使用:

foreach ($data as $item) {
    foreach ($item as $key => $value) {
        echo $user_data; 
        if ($user_data_is_max)
            echo "<trophy image>"; 
    }
}

甚至:

[0] => stdClass Object
    (
        [userid] => 1
        [week] => 1
        [sum] => 28
        [user] => al
        [trophy] => ""
    )
[1] => stdClass Object
    (
        [userid] => 1
        [week] => 2
        [sum] => 33
        [user] => al
        [trophy] => "<trophy image>"
    )

在循环上使用只打印(不需要if):

foreach ($data as $item) {
    foreach ($item as $key => $value) {
        echo $user_data . $user_data_trophy; 
    }
}