开发者

Drop Array Element and Sum Remaining Elements

开发者 https://www.devze.com 2023-03-10 16:37 出处:网络
I have an array that output\'s this: Array ( [0] => Array ( [week] => 1 ) [1] => Array ( [user] => 1 )

I have an array that output's this:

Array ( 
    [0] => Array ( [week] => 1 )
    [1] => Array ( [user] => 1 )
    [2] => Array ( [score] => 6 ) 

    [3] => Array ( [week] => 1 ) 
    [4] => Array ( [user] => 15 ) 
    [5] => Array ( [score] => 6 ) 

    [6] => Array ( [week] => 2 ) 
    [7] => Array ( [user] => 1 ) 
    [8] => Array ( [score] => 5 ) 

    [9] => Array ( [week] => 2 ) 
    [10] => Array ( [user] => 15 ) 
    [11] =&g开发者_JS百科t; Array ( [score] => 7 ) 

How do I drop the lowest score for user 1 (which would be 5 taken in week 2) from the array? I only want to drop the lowest number from the array for user 1 and then want to do the same thing for user 15 (which would be 6 in week 1).

Then, how would I sum the remaining scores in the array by user? There will be more weeks data to follow and I always want to drop the lowest per user for any week.

So thus far the total score for User 1 would be 6 thru 2 weeks and the total score for User 15 would be 7 thru week 2. If week 3 has lower scores for both users then their totals would be: User 1 = 11 and User 15 = 13.

Thanks in advance!!


Assuming that all records are in groups of 3:

$array = array(/* your data */);
$count = count($array) / 3;
$lowest = 0;
$min = PHP_INT_MAX;
for($i = 0; $i < $count; $i++)
    {
    if($array[$i + 1]['user'] == 1)
        {
        if($array[$i + 2]['score'] < $min)
            {
            $min = $array[$i + 2]['score'];
            $lowest = $i;
            }
        }
    }
unset($array[$lowest]);
unset($array[$lowest + 1]);
unset($array[$lowest + 2]);

Should at least give you the idea.


Your array structure is bizarre. Why is every array element itself a singleton array? Something like the following would make more sense:

$array_better = array (
    [0] => array (
        ['week'] => 1,
        ['user'] => 1,
        ['score'] => 6
    ),
    [1] => array (
        ['week'] => 1,
        ['user'] => 15,
        ['score'] => 6
    ),
    [2] => array (
        ['week'] => 2,
        ['user'] => 1,
        ['score'] => 5
    ),
    [3] => array (
        ['week'] => 2,
        ['user'] => 15,
        ['score'] => 7
    )
)

But for that matter, what would make still more sense would be a $array[$week][$user] structure, like this:

$array_wu = array (
    [1] => array (
        [1] => 6,
        [15] => 6
    ),
    [2] => array (
        [1] => 5,
        [15] => 7
    )
)

(If you do not understand what I have done here, please ask me about it.)

Alternatively again, you could have a $array[$user][$week] structure:

$array_uw = array (
    [1] => array (
        [1] => 6,
        [2] => 5
    ),
    [15] => array (
        [1] => 6,
        [2] => 7
    )
)

This in particular would make it very easy to do what you are trying to do - in fact it would take only one line of code statement:

$sum_excl_lowest = (array_key_exists(1, $array_uw) and count($array_uw[1])) ?
                   array_sum($array_uw[1]) - min($array_uw[1]) :
                   0;
0

精彩评论

暂无评论...
验证码 换一张
取 消