Menu

array from existing csv file

Jim Lill
2011-12-31
2012-09-07
  • Jim Lill

    Jim Lill - 2011-12-31

    My weather station creates a csv file like

    22:10,281,44,55,66,77,88,99,00,1010.9

    which is basically an APRS weather string with some math done on the last
    field of Baro Pressure. The first field is time in hes:min

    Is there a way to read that into my php code and only use certain fields or do
    I have to make an array from the array?

     
  • lbayuk

    lbayuk - 2012-01-01

    You will most likely have to read it in line by line and process and convert
    the fields to build a data array, except in the simplest of cases: if the
    first field (I assume you meant hrs:min) will be used as a data label, and you
    want something like a lines plot from all of the remaining fields. That would
    be 9 data sets, 9 lines, but all plotted to the same scale. That is probably
    not what you want.

    It isn't hard to read and process it. This example builds a data array with
    the first field as a label and 10th field as a value, for a single line or bar
    plot:

    $data = array();
    while ($a = fgetcsv($f)) {
        $data[] = array($a[0], $a[9]);
    }
    

    With a little more work, you could decode the hrs:min field and convert it to
    a time value. This would be useful if the samples are not at uniform intervals
    and you want to see that in the graph.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.