Connecting Tech Pros Worldwide Help | Site Map

searching and writing multidimensional arrays

Member
 
Join Date: Jun 2009
Posts: 52
#1: Jun 18 '09
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!=)
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#2: Jun 18 '09

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
Member
 
Join Date: Jun 2009
Posts: 52
#3: Jun 18 '09

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.  
Member
 
Join Date: Jun 2009
Posts: 52
#4: Jun 18 '09

re: searching and writing multidimensional arrays


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