Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question text file columns to arrays?

    i have a file called filename.txt with the csv as below

    Date,cet,predict,16d
    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:

    Code:
    // 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);
    Last edited by muppet77; 02-27-2012 at 05:34 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What version of PHP are you using?

  • #3
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Php 5.2

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    $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.

    Edit:
    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:
    PHP Code:
    $iCnt count($aEntries);
    for (
    $i 0$i $iCnt; ++$i)
    {
        
    $aParts explode(','$aEntries[$i]);
        
    $aResult[$i 1] = $aParts[1];

    Last edited by Fou-Lu; 02-27-2012 at 06:12 PM.

  • #5
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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 ...?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Like so (if you use the code above to build it):
    PHP Code:
    $aDates array_keys($aResult);
    $aCet array_values($aResult); 
    Edit:
    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.
    Last edited by Fou-Lu; 02-27-2012 at 06:13 PM.

  • Users who have thanked Fou-Lu for this post:

    muppet77 (02-27-2012)

  • #7
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    worked a treat!

    thank you so much Fou-Lu!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •