473,378 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 2947
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
8
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
by: assgar | last post by:
Hi Seasons Greetings Its back, I am being haunted. I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.