2014-12-29 125 views
1

這可能對你們中的一些人很簡單,但我真的在PHP中是虛擬的。我所需要的只是轉換一個多維數組並計算它的一些子數組值。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 
     ) 

它是簡單的獲得使用foreach[point]值的總和,但麻煩開始的時候我試圖讓與同一[mission]值陣列count。這就是我的代碼:

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); 

回答

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); 
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'))); 
0

首先,我覺得這條線給你添麻煩:

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

你應該設置初始計數爲0,因爲你見過沒有記錄至今:

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

之後你這樣做,每次你看到同樣的使命再創紀錄,你可以添加一個這樣的計數:

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

之後,你應該很好地計算平均值。

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']; 
} 
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