Distinct Items in an Array
| From: | Christopher J. Crane | Date: | Thu, 17 Apr 2003 13:25:13 +0000 |
| Subject: | Distinct Items in an Array | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-144282@lists.php.net to get a copy of this message | ||
I have a CSV type file that has a date field in it. The format is
"MM-DD-YYYY". I would like to break this up and push into an array the first
instance of a changed Month. For instance, if there is 14 instances of
"Feb", 21 instances of Mar and 4 instance of Jun, I only want the array to
have Feb in the first position and then have a NULL character for the next
13 instances, then Mar and then 20 NULL characters, then Jun and 3 NULL
characters.
This is the code I wrote and it gives me the opposite effect. What I get is
the first 13 instances in the array are blank and the last one is Feb
etc...I want the opposite, I need the first instance to be Feb, and the rest
to be blank until the new month comes along.
$Data = fopen("thefile.txt","r");
$LoopCount =1;
while ($Line = fgetcsv ($Data, 1000, ",")) {
if($LoopCount != 1) {
//Date,Open,High,Low,Close,Volume
array_push($Close, $Line[4]);
array_push($Open, $Line[1]);
list($DDate, $MDate, $YDate) = split("-", $Line[0]);
if($OldMDate != $MDate) { array_push($Date, $MDate); }
else { array_push($Date, ""); }
$OldMDate = $MDate;
}
$LoopCount++;
}