473,608 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 calculateAverag eRating($rating _matrix)
{
$average_rating = array();
foreach($rating _matrix as $cus_id => $ratings)
{
$counter = 0;
$sum = 0;
foreach($rating s 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($averag e_rating as $key => $value);
{
echo "<td>$key::$val ue</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 2982
"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 calculateAverag eRating($rating _matrix)
{
$average_rating = array();
foreach($rating _matrix as $cus_id => $ratings)
{
$counter = 0;
$sum = 0;
foreach($rating s 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($averag e_rating as $key => $value);
{
echo "<td>$key::$val ue</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 calculateAverag eRating($rating _matrix)
{
$average_rating = array();
foreach($rating _matrix as $cus_id => $ratings){
$average_rating[$cus_id] = (array_sum($rat ings) / count($ratings) );
}
echo "<hr>";
echo "<table> <tr>";
foreach($averag e_rating as $key => $value){
echo "<td>$key::$val ue</td>";
}
echo "</tr></table>";
return $average_rating ;
}
This should work if $rating_matrix looks like this:

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

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

I believe the reason your function didn't work was because of this line:
foreach($averag e_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***@glassgian t.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
3254
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)) CType(local4, Short) = CType(src, Short)
9
4786
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 am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
5
2204
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 1,1,2,4. I have narrowed it down to the sort function. I'm almost positive
8
4604
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 later on. Before that happens there has been a problem that causes a crash of the program. This is a little program "test.exe" I wrote to test what happens. compiler: mingw 3.1.01 for win32 command line
12
5500
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 following program: #include<stdio.h> #define SIZE 1 int main()
204
12979
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 = {0,1,2,4,9};
8
10706
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 receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
104
16913
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 that array Could you show me a little example how to do this? Thanks.
23
7391
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
4
1977
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) a display function on the HTML form and
0
8071
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8013
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8503
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8360
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3977
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1613
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.