adding up value of array and getting the average

Posted by sea_1987 on Stack Overflow See other posts from Stack Overflow or by sea_1987
Published on 2010-06-02T10:45:53Z Indexed on 2010/06/02 10:53 UTC
Read the original article Hit count: 283

Filed under:
|
|

I have an array that looks similar to this,

[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
            (
            )

    )

I wanting to add up the [rating] and get the mean average. But I dont know how do this with PHP, my attempt looks like this,

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

© Stack Overflow or respective owner

Related posts about php

Related posts about math