470,848 Members | 1,830 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

array problem

hi !

i m trying to calculate an average array from an 2d data array.
my problem is that my function does not generate an average array but just
one value.
but i do not understand why it does not work.

code:
-----

function calculateAverageRating($rating_matrix)
{
$average_rating = array();
foreach($rating_matrix as $cus_id => $ratings)
{
$counter = 0;
$sum = 0;
foreach($ratings as $pro_id => $item_rate)
{
$counter++;
$sum += $item_rate;
}
echo $cus_id."->".($sum/$counter)."<br>";
$average_rating[$cus_id] = ($sum / $counter);
}
echo "<hr>";
echo "<table> <tr>";
foreach($average_rating as $key => $value);
{
echo "<td>$key::$value</td>";
}
echo "</tr></table>";
return $average_rating;
}
wanted:

input -> output

1 1 1 1
2 2 2 2
4 4 4 4

but my functions just does:

input -> output

1 1 1
2 2 2
4 4 4 4

for any help thankful

Helmut
Jul 17 '05 #1
1 2719
"H. Adam" wrote:

hi !

i m trying to calculate an average array from an 2d data array.
my problem is that my function does not generate an average array but just
one value.
but i do not understand why it does not work.

code:
-----

function calculateAverageRating($rating_matrix)
{
$average_rating = array();
foreach($rating_matrix as $cus_id => $ratings)
{
$counter = 0;
$sum = 0;
foreach($ratings as $pro_id => $item_rate)
{
$counter++;
$sum += $item_rate;
}
echo $cus_id."->".($sum/$counter)."<br>";
$average_rating[$cus_id] = ($sum / $counter);
}
echo "<hr>";
echo "<table> <tr>";
foreach($average_rating as $key => $value);
{
echo "<td>$key::$value</td>";
}
echo "</tr></table>";
return $average_rating;
}

wanted:

input -> output

1 1 1 1
2 2 2 2
4 4 4 4

but my functions just does:

input -> output

1 1 1
2 2 2
4 4 4 4


function calculateAverageRating($rating_matrix)
{
$average_rating = array();
foreach($rating_matrix as $cus_id => $ratings){
$average_rating[$cus_id] = (array_sum($ratings) / count($ratings));
}
echo "<hr>";
echo "<table> <tr>";
foreach($average_rating as $key => $value){
echo "<td>$key::$value</td>";
}
echo "</tr></table>";
return $average_rating;
}
This should work if $rating_matrix looks like this:

"cus_id1"=>array(int1, int2,...,intN),
"cus_id2"=>array(int1, int2,...,intN),
....,
"cus_idN"=>array(int1, int2,...,intN),

If it doesn't look like that, please print_r($your_array) and post it.

I believe the reason your function didn't work was because of this line:
foreach($average_rating as $key => $value);

Your script was interpreting the ";" as the statement to execute in the
foreach. The foreach therefore did nothing. Then the script executed what you
had intended to be the foreach codeblock (the stuff between "{" and "}") once,
as it wasn't within a loop.

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com

I have a spam filter. Please include "PHP" in the
subject line to ensure I'll get your message.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Federico G. Babelis | last post: by
5 posts views Thread by ritchie | last post: by
8 posts views Thread by Gerald | last post: by
204 posts views Thread by Alexei A. Frounze | last post: by
8 posts views Thread by intrepid_dw | last post: by
104 posts views Thread by Leszek | last post: by
4 posts views Thread by assgar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.