473,320 Members | 1,848 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,320 software developers and data experts.

searching and writing multidimensional arrays

So what I am trying to do is read through a document that looks like this.

testA phaseA 5
testA phaseB 3
testA phaseA 4
testB phaseA 4

so right now, it's going to read through the document line by line and exploding it. I want it to be able to put the information in a multidimensional array looking something like this
Expand|Select|Wrap|Line Numbers
  1. array(
  2.   [0]=>testA array(
  3.                       [0]=>phaseA = 9,
  4.                       [1]=>phaseB = 3
  5.          ),
  6.   [1]=>testB array(
  7.                       [0]=>phaseA = 4
  8.         )
  9.      )
  10.  
So basically, if it sees that the test and phase already exists, it just adds the number of hours to the previous number. Any help would be appreciated. I'm not sure how to go about this. A sample code would probably be helpful. Ultimately, after writing this array, I need to be able to print out the number of hours of each phase as well as the total number of hours for each test(adding up all the hours for each phase in it). Thanks so much!=)
Jun 18 '09 #1
3 1770
dlite922
1,584 Expert 1GB
I'm feeling generous today, here you go buddy:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $file = array("testA phaseA 5","testA phaseB 3","testA phaseA 4","testB phaseA 4");
  3. $resultArray = array(); 
  4.  
  5. foreach ($file as $line) {
  6.     list($test, $phase, $value) = explode (" ",$line); 
  7.  
  8.     $resultArray[$test][$phase] += $value;
  9.  
  10. }
  11.  
  12. print_r($resultArray); 
  13.  
  14.  
outputs:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Array
  3. (
  4.     [testA] => Array
  5.         (
  6.             [phaseA] => 9
  7.             [phaseB] => 3
  8.         )
  9.  
  10.     [testB] => Array
  11.         (
  12.             [phaseA] => 4
  13.         )
  14.  
  15. )
  16.  
  17.  
go nuts,





Dan
Jun 18 '09 #2
hm..so im trying it out with my coding rite now and actually..my document was more like
testA.phaseA=3.0 % testing
for each line.
which is why i couldnt just separate it with exploding spaces. But I did manage to separate them and all of them have a different variable name. Unfortuatnely, when I run my coding, it only shows testB. I believe it overwrote my previous data? Here is my coding.

Expand|Select|Wrap|Line Numbers
  1. $parts2=explode(' % ',$invalidcheck);
  2. $parts3=explode('=', $parts2[0]);
  3. $parts4=explode('.', $parts3[0]);
  4. list($project, $phase) = $parts4;
  5. $project=$parts4[0];
  6. $phase=$parts4[1];
  7. $description=$parts2[1];
  8. $hours=$parts3[1];
  9. $database=array();
  10. $database[$project][$phase]+=$hours;
  11.  
Jun 18 '09 #3
nvm..it works now. I initialized my array at the wrong spot.=P
Jun 18 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
2
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Scott Handojo | last post by:
Lets say I have the following arrays X1 (1, 2, 7, 8) X2 (1, 2, 4, 6) X3 (1, 2, 3, 5) X4 (1, 3, 5) X5 (2, 3, 5) X6 (4, 5, 6, 8) My 'search array' is comprised of
3
by: Claire | last post by:
I have a multidimensional array defined as private double myArray = new double; The first column of the array contains X values, the other contains Y values I have a charting function defined as...
3
by: Ravi Singh (UCSD) | last post by:
Hello all I am trying to use jagged and multi-dimensional arrays in C++. In C# these work fine // for jagged arrays string jaggedArray = new string ; //for multidimensional arrays string...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.