PHP:按数组计算数组值

时间:2014-12-29 09:17:04

标签: php arrays foreach count sum

对于你们这些人来说这可能很简单,但我在php中真的很虚伪。我所需要的只是转换一个多维数组并计算它的一些子数组值。

  Array
    (
        [0] => Array
            (
                [date] => 2014-10-30
                [mission] => one
                [point] => 10
            )

        [1] => Array
            (
                [date] => 2014-10-31
                [mission] => five
                [point] => 10
            )

        [2] => Array
            (
                [date] => 2014-11-19
                [mission] => one
                [point] => 8
            )

输出结果如下:

 Array
(
    [one] => Array
        (
            [mission] => one
            [point] => 18 // sum all points where [mission] => one
            [count] => 2
            [everage] => 9 // 18/2
        )

    [five] => Array
        (
            [mission] => five
            [point] => 10
            [count] => 1
            [everage] => 10
        )

使用[point]获取foreach值的总和很简单,但当我尝试获取count具有相同[mission]值的数组时,会出现问题。那是我的代码:

foreach($missionsarray as $row) {
  if(!isset($newarray[ $row['mission'] ])) {
       $newarray[ $row['mission'] ] = $row;
       $newarray[ $row['mission'] ]['count'] = count($row['point']);
       continue ;
   }
   $newarray[ $row['mission'] ]['point'] += $row['point'];
}

print_r($newarray);

5 个答案:

答案 0 :(得分:2)

count()的参数应该是一个数组,它返回该数组中的元素数。那不是你想要的。当您首次在结果中为任务创建新条目时,应将count设置为1。然后,每次向其添加另一个point时,只需将其递增。

foreach($missionsarray as $row) {
  if(!isset($newarray[ $row['mission'] ])) {
       $newarray[ $row['mission'] ] = $row;
       $newarray[ $row['mission'] ]['count'] = 1;
       continue ;
   }
   $newarray[ $row['mission'] ]['point'] += $row['point'];
   $newarray[ $row['mission'] ]['count']++;
}

print_r($newarray);

答案 1 :(得分:0)

使用此

$newarray[ $row['mission'] ]['point'] += $row['point'];
$newarray[ $row['mission'] ]['count']++;

print_r($newarray);

PHP 5.5更新

array_column()

$counts = array_count_values(array_flip(array_column($array, 'point')));

答案 2 :(得分:0)

首先,我认为这一行会给你带来麻烦:

$newarray[ $row['mission'] ]['count'] = count($row['point']);

您应该将初始计数设置为0,因为到目前为止您还没有看到任何记录:

$newarray[ $row['mission'] ]['count'] = 0;

执行此操作后,每当您看到具有相同任务的其他记录时,您可以像这样在计数中添加一个:

$newarray[ $row['mission'] ]['point'] += $row['point'];
$newarray[ $row['mission'] ]['count']++;

之后你应该很好地计算平均值。

答案 3 :(得分:0)

我认为您尝试做的事情如下:

foreach ($missionsarray as $row) {
    $mission = $row['mission'];
    $point   = $row['point'];

    if (! isset($newarray[$mission])) {
        $newarray[$mission] = [
            'mission' => $mission,
            'point'   => 0,
            'count'   => 0,
            'average' => 0
        ];
    }

    $newarray[$mission]['point'] += $point;
    $newarray[$mission]['count']++;

    $newarray[$mission]['average'] =
        $newarray[$mission]['point'] / $newarray[$mission]['count'];
}

答案 4 :(得分:0)

我认为使用for循环解决这个问题可能更容易,如下所示:

<?php

$arr = array(0 => array("date" => "2014-10-30","mission" => "one","point" => 10),
             1 => array("date" => "2014-10-31","mission" => "five","point" => 10),
             2 => array("date" => "2014-11-19","mission" => "one","point" => 8)
             );

$ototal = 0; // total mission one points
$ftotal = 0; // total mission five points

$ocount = 0; // count for mission one
$fcount = 0; // count for mission five

for ($i=0, $max = count( $arr ); $i < $max; $i++){
    if ($arr[$i]["mission"] == "one"){
        $ototal += $arr[$i]["point"];
        $ocount++;

    }
    else
    if ($arr[$i]["mission"] == "five"){
        $ftotal += $arr[$i]["point"];
        $fcount++;
    }
}

// transform $arr by adding the following elements:
$arr["one"] = array("mission" => "one", "point" => $ototal, "count" => $ocount, "average" => $ototal/ $ocount);
$arr["five"]= array("mission" => "five","point" => $ftotal, "count" => $fcount, "average" => $ftotal/$fcount);

var_dump($arr["one"],$arr["five"]);

现场演示here