473,320 Members | 1,969 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.

looping data into arrays

I am currently trying to put my information into a multidimensional array, but for some reason, when I print_r the array, only the last inputed data shows. My current coding is like this: (I cut off the top portion where I'm opening the file and whatnot)

Expand|Select|Wrap|Line Numbers
  1. while (! feof($codes)){
  2.             $lines = fgets($codes);
  3.             $lines=preg_replace('/\s\s+/',' ', $lines);            
  4.              $parts = explode(" ", $lines);
  5.             $projectname = explode ('.', $parts[0]);
  6.             $datetemp=substr($parts[2],1,13);
  7.             $projectdate = explode ('-', $datetemp);
  8.                         $try1=$projectdate[0];
  9.                         $try2=$projectdate[1];            
  10.  
  11.                         if ($try1{0}=='0'){
  12.                 $try1=substr($projectdate[0],1,5);
  13.                 }
  14.             $try2=$projectdate[1];
  15.             if ($try2{0}=='0'){
  16.                 $try2=substr($projectdate[1],1,5);
  17.                 }
  18.             $valid1[]=$try1;
  19.             $valid2[]=$try2;
  20.  
  21.             //assuming no dates before 2000
  22.             if ($todaydate<=$try2 && $todaydate>=$try1){
  23.                 $validproj[$x]=$projectname[0];
  24.                 if ($validproj[--$x]!==$projectname[0])
  25.                 {$x++;}
  26.                 $validcode[$y]=$projectname[1];
  27. $x++;
  28.  
  29.             $data=array("$projectname[0]"=>array("$i"=>$validcode[$y]));            
  30.             }
  31. $i++;
  32. $y++;
  33.     }
  34.  
  35. fclose($codes);
  36. print_r($data);
  37.  
  38. ?>
It currently shows:

Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [projB] => Array
  4.         (
  5.             [3] => codeD
  6.         )
  7.  
  8. )

where as it should show this:

Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [projA] => Array
  4.         (
  5.             [0] => codeA
  6.             [1] => codeB
  7.             [2] => codeC
  8.  
  9.         )
  10.  
  11.     [projB] => Array
  12.         (
  13.             [3] => codeD
  14.         )
  15.  
  16. )

Can anyone shed some light on my problem??Thank in advance!=)
Jun 4 '09 #1
2 1231
Dormilich
8,658 Expert Mod 8TB
it looks like you overwrite the $data array (line 29) in every loop. but you need to append the data. e.g.
Expand|Select|Wrap|Line Numbers
  1. $array = array('Key' => 'Value'); // defines always a new array
  2. $array['newKey'] = 'newValue';
Jun 4 '09 #2
oh..lol..thank you very much=)
Jun 4 '09 #3

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

Similar topics

4
by: David | last post by:
Hello. I am looking for advice on what is "best practice" regarding looping through a form to check its checkboxes and associated data fields. Here is what I am trying to do (Here is the page...
27
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
5
by: john | last post by:
Hello, I am trying to send 2^19 data to USB2.0. I am reading the file "all_sines" and loading it into an array called "string1". Then, in a for loop I copied it into the USB data array...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
11
by: motion musso aka: sathia | last post by:
this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length<i;i++){ do_something(); //i need the current myarray } bye bye
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
6
by: stefano.troiani | last post by:
Hi, i have two arrays and I would like to loop both of them one inside the other. The first one comes from a readdir and present an array with the document's names, the second one is an array...
1
by: assgar | last post by:
Hello I have changed the process code abit so it receives the data from the form and ensures the data in array format. This has eliminated my previous error. The problem I am experiencing...
4
by: nico3334 | last post by:
I'm having problems with my code concerning a mulitdimensional array. Basically, I'm trying to create an array (arrTotal) which contains all of the data from 2 arrays (arrOne & arrTwo). Basically,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.