Connecting Tech Pros Worldwide Help | Site Map

searching and writing multidimensional arrays

  #1  
Old June 18th, 2009, 03:52 PM
Member
 
Join Date: Jun 2009
Posts: 52
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!=)
  #2  
Old June 18th, 2009, 06:02 PM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,075

re: searching and writing multidimensional arrays


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
  #3  
Old June 18th, 2009, 06:39 PM
Member
 
Join Date: Jun 2009
Posts: 52

re: searching and writing multidimensional arrays


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.  
  #4  
Old June 18th, 2009, 08:31 PM
Member
 
Join Date: Jun 2009
Posts: 52

re: searching and writing multidimensional arrays


nvm..it works now. I initialized my array at the wrong spot.=P
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM