"Collapse" a file using PHP array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DesignGuy

    #1

    "Collapse" a file using PHP array?

    For lack of a better term I wish to collapse a data file as so:

    03/01/04|item1|3081|| |
    03/01/04|item2|2081|| |
    03/01/04|item3|1195|| |

    03/01/04|item1||556||
    03/01/04|item2||450||
    03/01/04|item3||397||

    03/01/04|item1|||434|
    03/01/04|item2|||544|
    03/01/04|item3|||232|

    03/01/04|item1||||343
    03/01/04|item2||||545
    03/01/04|item3||||433

    so that it would read:

    03/01/04|item1|3081|5 56|434|343
    03/01/04|item2|2081|4 50|544|545
    03/01/04|item3|1195|3 97|232|433

    (data would extend beyond 03/01/04, so subsequent dates must be taken into
    account)

    My idea is to import into an array, and then "collapse" it somehow - but
    how? Is it possible without numerious iterations?

    Many thanks in advance.



  • Reply Via Newsgroup

    #2
    Re: "Collapse& quot; a file using PHP array?

    DesignGuy wrote:
    [color=blue]
    > For lack of a better term I wish to collapse a data file as so:
    >
    > 03/01/04|item1|3081|| |
    > 03/01/04|item2|2081|| |
    > 03/01/04|item3|1195|| |
    >
    > 03/01/04|item1||556||
    > 03/01/04|item2||450||
    > 03/01/04|item3||397||
    >
    > 03/01/04|item1|||434|
    > 03/01/04|item2|||544|
    > 03/01/04|item3|||232|
    >
    > 03/01/04|item1||||343
    > 03/01/04|item2||||545
    > 03/01/04|item3||||433
    >
    > so that it would read:
    >
    > 03/01/04|item1|3081|5 56|434|343
    > 03/01/04|item2|2081|4 50|544|545
    > 03/01/04|item3|1195|3 97|232|433
    >
    > (data would extend beyond 03/01/04, so subsequent dates must be taken into
    > account)
    >
    > My idea is to import into an array, and then "collapse" it somehow - but
    > how? Is it possible without numerious iterations?
    >
    > Many thanks in advance.
    >[/color]

    Check out http://ca2.php.net/fgetcsv

    It should allow you to read the file in to an array without conversion -
    It expects a comma as a field seperator, however if your data above is
    exact, you should tell tell it that you have the pipe character as
    delimiter.

    randelld

    Comment

    • Pedro Graca

      #3
      Re: "Collapse& quot; a file using PHP array?

      DesignGuy wrote:[color=blue]
      > For lack of a better term I wish to collapse a data file as so:
      >
      > 03/01/04|item1|3081|| |
      > 03/01/04|item2|2081|| |[/color]
      (snip)
      [color=blue]
      > so that it would read:
      >
      > 03/01/04|item1|3081|5 56|434|343
      > 03/01/04|item2|2081|4 50|544|545
      > 03/01/04|item3|1195|3 97|232|433
      >
      > (data would extend beyond 03/01/04, so subsequent dates must be taken into
      > account)
      >
      > My idea is to import into an array, and then "collapse" it somehow - but
      > how? Is it possible without numerious iterations?[/color]


      Using fgetcsv() or some other file reading function I'd try to put that
      data into an array of arrays with the format

      $arr['03/01/04']['item1'][0] = 3081;
      $arr['03/01/04']['item1'][1] = 556;
      $arr['03/01/04']['item1'][2] = 434;
      $arr['03/01/04']['item1'][3] = 343;

      $arr['03/01/04']['item2'][0] = 2081;
      $arr['03/01/04']['item2'][1] = 450;
      $arr['03/01/04']['item2'][2] = 544;
      $arr['03/01/04']['item2'][3] = 545;

      ....

      and after that, to write the array to another file, I'd use foreach()
      and implode()

      foreach ($arr as $dat=>$items) {
      foreach ($items as $item=>$data) {
      echo $dat, '|', $item, '|', implode('|', $data);
      }
      }


      Check these functions at the manual:
      Gets line from file pointer and parse for CSV fields





      HTH
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      Working...