View Full Version : text file columns to arrays?

02-27-2012, 06:31 PM
i have a file called filename.txt with the csv as below

Wed 02/15 @ 12Z,0.7,3.4,x
Wed 02/15 @ 18Z,0.7,3.3,x
Thu 02/16 @ 00Z,0.7,3.3,x
Thu 02/16 @ 06Z,1.1,3.7,x
Thu 02/16 @ 12Z,1.1,3.7,x

i would like the date to be replaced by numbers 1,2,3... and to be an array, and also the 2nd value in each row to be an array.

the number of rows is not fixed and grows day by day.

so the result would be (1,2,3,4,5) and (0.7,0.7,0.7,1.1,1.1).

can someone please help?

i guessed that some sort of loop is needed to go through row by row, adding values to an array.

I have tried the following with no success:

// get data from the file
$data = file_get_contents('filename.txt');

// use the newline as a delimiter to get different rows
$rows = explode("\n", $data);
// go through all the rows starting at the second row
// remember that the first row contains the headings
for($i = 1; $i < count($rows); $i++){
$temp = explode(',', $rows[$i]);
$date = $temp[0];
$cet = $temp[1];
$stack = array($stack);
array_push($stack, $cet);}
print_r ($stack);

02-27-2012, 06:35 PM
What version of PHP are you using?

02-27-2012, 06:37 PM
Php 5.2

02-27-2012, 06:57 PM
Sorry that was irrelevant, I was thinking of fputcsv, fgetcsv is available since 4.x. Decided that it wouldn't help much anyway.
There are several ways to do what you are looking to do. The easiest I can think of is to use file() and flatten it. The number you have is simply the offset, so I would use that to my advantage.

$aEntries = file('filename.txt', FILE_IGNORE_NEW_LINES);
// In case you want to keep the header for whatever
$sHeader = array_shift($aEntries);

foreach ($aEntries AS $entry)
$aParts = explode(",", $entry);
$aResult[] = $aParts[1];

That will return a key => value pair that matches your above. You can split the two using array_keys and array_values if you like.

Sorry PHP's 0 based, you'll need to force an increment if you want to keep it as 1 - 5 for example. That can replace the loop with so:

$iCnt = count($aEntries);
for ($i = 0; $i < $iCnt; ++$i)
$aParts = explode(',', $aEntries[$i]);
$aResult[$i + 1] = $aParts[1];

02-27-2012, 07:06 PM
thanks Fou-Lu

I do not really know what you mean about accessing the data arrays. I am new to arrays and keen to learn. I sort of get the idea about keys etc.

Ultimately I would like the date 'numbers' (1,2,3 etc) in one array and the second column in another.

what do i use to 'get' them?

echo ...?

02-27-2012, 07:11 PM
Like so (if you use the code above to build it):

$aDates = array_keys($aResult);
$aCet = array_values($aResult);

Also, I modified the above. I had two array_shifts, you should only have one. The second was without a capture, but I decided to keep the header in case you want to use it for something.
Also, echo is used to print to the client. To print an array you need to iterate it as a print $array; always results in 'Array' if it is an array. You need to specify an offset with $array['offset'], or iterate with a for, foreach or while loop and print each item individually.

02-27-2012, 07:21 PM
worked a treat!

thank you so much Fou-Lu!