2010-06-02 74 views
0

我有一個數組,看起來與此類似,加起來陣列的價值並獲得平均

[4] => Common_Model Object 
    (
     [id] => 4 
     [name] => 
     [date_created] => 
     [last_updated] => 
     [user_id_updated] => 
     [_table] => 
     [_aliases] => Array 
      (
       [id] => 4 
       [name] => 
       [date_created] => 
       [date_updated] => 
       [user_id_updated] => 
       [rating] => 3 
       [recipe_id] => 5 
      ) 

     [_nonDBAliases] => Array 
      (
      ) 

     [_default] => Array 
      (
      ) 

     [_related] => Array 
      (
      ) 

     [_enums] => 
     [_alsoDelete] => Array 
      (
      ) 

     [_readOnly] => Array 
      (
       [0] => date_updated 
      ) 

     [_valArgs] => Array 
      (
      ) 

     [_valArgsHash] => Array 
      (
       [default] => Array 
        (
        ) 

      ) 

     [_valAliases] => Array 
      (
      ) 

     [_extraData] => Array 
      (
      ) 

     [_inputs] => Array 
      (
      ) 

     [_tableName] => jm_ratings 
     [_tablePrefix] => 
     [_niceDateUpdated] => 1st Jan 70 
     [_niceDateCreated] => 1st Jan 70 
     [_fetchAdminData] => 
     [_mCache] => 
     [_assets] => Array 
      (
      ) 

    ) 

[3] => Common_Model Object 
    (
     [id] => 3 
     [name] => 
     [date_created] => 
     [last_updated] => 
     [user_id_updated] => 
     [_table] => 
     [_aliases] => Array 
      (
       [id] => 3 
       [name] => 
       [date_created] => 
       [date_updated] => 
       [user_id_updated] => 
       [rating] => 1 
       [recipe_id] => 5 
      ) 

     [_nonDBAliases] => Array 
      (
      ) 

     [_default] => Array 
      (
      ) 

     [_related] => Array 
      (
      ) 

     [_enums] => 
     [_alsoDelete] => Array 
      (
      ) 

     [_readOnly] => Array 
      (
       [0] => date_updated 
      ) 

     [_valArgs] => Array 
      (
      ) 

     [_valArgsHash] => Array 
      (
       [default] => Array 
        (
        ) 

      ) 

     [_valAliases] => Array 
      (
      ) 

     [_extraData] => Array 
      (
      ) 

     [_inputs] => Array 
      (
      ) 

     [_tableName] => jm_ratings 
     [_tablePrefix] => 
     [_niceDateUpdated] => 1st Jan 70 
     [_niceDateCreated] => 1st Jan 70 
     [_fetchAdminData] => 
     [_mCache] => 
     [_assets] => Array 
      (
      ) 

    ) 

[2] => Common_Model Object 
    (
     [id] => 2 
     [name] => 
     [date_created] => 
     [last_updated] => 
     [user_id_updated] => 
     [_table] => 
     [_aliases] => Array 
      (
       [id] => 2 
       [name] => 
       [date_created] => 
       [date_updated] => 
       [user_id_updated] => 
       [rating] => 1 
       [recipe_id] => 5 
      ) 

     [_nonDBAliases] => Array 
      (
      ) 

     [_default] => Array 
      (
      ) 

     [_related] => Array 
      (
      ) 

     [_enums] => 
     [_alsoDelete] => Array 
      (
      ) 

     [_readOnly] => Array 
      (
       [0] => date_updated 
      ) 

     [_valArgs] => Array 
      (
      ) 

     [_valArgsHash] => Array 
      (
       [default] => Array 
        (
        ) 

      ) 

     [_valAliases] => Array 
      (
      ) 

     [_extraData] => Array 
      (
      ) 

     [_inputs] => Array 
      (
      ) 

     [_tableName] => jm_ratings 
     [_tablePrefix] => 
     [_niceDateUpdated] => 1st Jan 70 
     [_niceDateCreated] => 1st Jan 70 
     [_fetchAdminData] => 
     [_mCache] => 
     [_assets] => Array 
      (
      ) 

    ) 

我想加起來[rating]並獲得平均的平均水平。但我不知道如何用PHP做到這一點,我的嘗試是這樣的,

<?php 
    foreach ($rt as $rating) { 
      $total = $rating->rating + $rating->rating 
    } 
$total/count($rt); 
?> 

回答

1

試試這個:

$total = 0; 
foreach($rt as $elem) { 
    $total += $elem->_aliases['rating']; 
} 
echo sprintf("Average: %d", $total/count($rt)); 
+0

這是一個對象,你就必須通過$訪問_aliases財產因爲它是公共財產,所以elem - > _別名。 – 2010-06-02 10:50:35

+0

你是對的,謝謝你的注意。 – halfdan 2010-06-02 10:53:14

1
<?php 
    $total = 0; 
    foreach ($rt as $obj) { 
     $total += $obj->_aliases['rating']; 
    } 
    print $total/count($rt); 
?>