tags:

views:

42

answers:

2

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);
?>
+1  A: 

Try this:

$total = 0;
foreach($rt as $elem) {
    $total += $elem->_aliases['rating'];
}
echo sprintf("Average: %d", $total/count($rt));
halfdan
This is an object, you'd have to access _aliases property by $elem->_aliases, given that it's public property.
mr.b
You are right, thanks for noticing.
halfdan
+1  A: 
<?php
    $total = 0;
    foreach ($rt as $obj) {
         $total += $obj->_aliases['rating'];
    }
    print $total / count($rt);
?>
Flavius Stef