431,965 Members | 2,043 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,965 IT Pros & Developers. It's quick & easy.

# array problem

 P: n/a 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)."
"; \$average_rating[\$cus_id] = (\$sum / \$counter); } echo "
"; echo "
\$key::\$value
"; foreach(\$average_rating as \$key => \$value); { echo ""; } echo ""; 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

 P: n/a "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)."
"; \$average_rating[\$cus_id] = (\$sum / \$counter); } echo "
"; echo "
\$key::\$value
"; foreach(\$average_rating as \$key => \$value); { echo ""; } echo ""; 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 "
"; echo "
\$key::\$value
"; foreach(\$average_rating as \$key => \$value){ echo ""; } echo ""; 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. 