Re: Distinct Items in an Array
| From: | Christopher J. Crane | Date: | Thu, 17 Apr 2003 13:52:27 +0000 |
| Subject: | Re: Distinct Items in an Array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-144284@lists.php.net to get a copy of this message | ||
One way I thought to do this might be to loop through the file 2 times
The first time I would push the months into an array and then use
array_unique to get distinct month names.
Then loop through the file a second time to match months from the unique
array to the $Date array.
"Christopher J. Crane" <ccrane@ikon.com> wrote in message
news:20030417132507.22437.qmail@pb1.pair.com...
> 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++;
> }
>
>