CSV to associative array

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

    CSV to associative array

    Hi all,

    Kind of a PHP newbie and was wondering if anyone could provide some
    guidance. I have 2 CSV files with 2 columns each and want to find the
    intersection of both based on the value in the first column. For
    example

    File 1

    id1, apple
    id2, orange
    id3, banana
    id4, durian
    id5, tomato

    File 2

    id7, coke
    id6, drpepper
    id5, sprite
    id4, 7up
    id1, water

    My desired output is:

    id1, apple, water
    id4, durian, 7up
    id5, tomato, sprite

    I've been trying to create some PHP scripts to do that, using merge,
    usort, array_intersect . I think my main problem is that when use
    fgetcsv, I'm creating multi-dimensional arrays.

    I *think* what I should be doing is creating single dimension arrays
    with IDs as keys, and the fruit/drink as values. Then I should be able
    to use array_intersect _assoc to compare the keys from each list and
    match IDs.

    However, I don't know how to go from id1, apple to array(id1 =>
    apple). Anyone know how I can read the CSV files in this way? Am i on
    the right track?

    Thanks!
  • C. (http://symcbean.blogspot.com/)

    #2
    Re: CSV to associative array

    On May 20, 12:34 pm, rotron <roshanv...@gma il.comwrote:
    Hi all,
    >
    Kind of a PHP newbie and was wondering if anyone could provide some
    guidance. I have 2 CSV files with 2 columns each and want to find the
    intersection of both based on the value in the first column. For
    example
    >
    File 1
    >
    id1, apple
    id2, orange
    id3, banana
    id4, durian
    id5, tomato
    >
    File 2
    >
    id7, coke
    id6, drpepper
    id5, sprite
    id4, 7up
    id1, water
    >
    My desired output is:
    >
    id1, apple, water
    id4, durian, 7up
    id5, tomato, sprite
    >
    I've been trying to create some PHP scripts to do that, using merge,
    usort, array_intersect . I think my main problem is that when use
    fgetcsv, I'm creating multi-dimensional arrays.
    >
    I *think* what I should be doing is creating single dimension arrays
    with IDs as keys, and the fruit/drink as values. Then I should be able
    to use array_intersect _assoc to compare the keys from each list and
    match IDs.
    >
    However, I don't know how to go from id1, apple to array(id1 =>
    apple). Anyone know how I can read the CSV files in this way? Am i on
    the right track?
    >
    Thanks!
    No - FFS use a relational DBMS! They are designed specifically to
    solve this problem.
    SQLite now comes as standard with PHP.

    C.

    Comment

    • Erwin Moller

      #3
      Re: CSV to associative array

      rotron schreef:
      Hi all,
      >
      Kind of a PHP newbie and was wondering if anyone could provide some
      guidance. I have 2 CSV files with 2 columns each and want to find the
      intersection of both based on the value in the first column. For
      example
      >
      File 1
      >
      id1, apple
      id2, orange
      id3, banana
      id4, durian
      id5, tomato
      >
      File 2
      >
      id7, coke
      id6, drpepper
      id5, sprite
      id4, 7up
      id1, water
      >
      My desired output is:
      >
      id1, apple, water
      id4, durian, 7up
      id5, tomato, sprite
      >
      I've been trying to create some PHP scripts to do that, using merge,
      usort, array_intersect . I think my main problem is that when use
      fgetcsv, I'm creating multi-dimensional arrays.
      >
      I *think* what I should be doing is creating single dimension arrays
      with IDs as keys, and the fruit/drink as values. Then I should be able
      to use array_intersect _assoc to compare the keys from each list and
      match IDs.
      >
      However, I don't know how to go from id1, apple to array(id1 =>
      apple). Anyone know how I can read the CSV files in this way? Am i on
      the right track?
      >
      Thanks!
      Hi,

      Well, for starters, this should be done in the database.
      Writing a query that produces excactly that is easy.

      So before helping out with your current approach (the flat files) I have
      a question: Can you go back to the database and solve it over there?

      Regards,
      Erwin Moller

      Comment

      • rotron

        #4
        Re: CSV to associative array

        On May 20, 5:15 pm, Erwin Moller
        <Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
        rotron schreef:
        >
        >
        >
        Hi all,
        >
        Kind of a PHP newbie and was wondering if anyone could provide some
        guidance. I have 2 CSV files with 2 columns each and want to find the
        intersection of both based on the value in the first column. For
        example
        >
        File 1
        >
        id1, apple
        id2, orange
        id3, banana
        id4, durian
        id5, tomato
        >
        File 2
        >
        id7, coke
        id6, drpepper
        id5, sprite
        id4, 7up
        id1, water
        >
        My desired output is:
        >
        id1, apple, water
        id4, durian, 7up
        id5, tomato, sprite
        >
        I've been trying to create some PHP scripts to do that, using merge,
        usort, array_intersect . I think my main problem is that when use
        fgetcsv, I'm creating multi-dimensional arrays.
        >
        I *think* what I should be doing is creating single dimension arrays
        with IDs as keys, and the fruit/drink as values. Then I should be able
        to use array_intersect _assoc to compare the keys from each list and
        match IDs.
        >
        However, I don't know how to go from id1, apple to array(id1 =>
        apple). Anyone know how I can read the CSV files in this way? Am i on
        the right track?
        >
        Thanks!
        >
        Hi,
        >
        Well, for starters, this should be done in the database.
        Writing a query that produces excactly that is easy.
        >
        So before helping out with your current approach (the flat files) I have
        a question: Can you go back to the database and solve it over there?
        >
        Regards,
        Erwin Moller
        Hi Erwin,
        Thanks for your reply.
        The 2 CSV files are straight from Excel. I could do a Vlookup to get
        the desired result, but am trying to automate this as much as possible.

        Comment

        • Erwin Moller

          #5
          Re: CSV to associative array

          rotron schreef:
          On May 20, 5:15 pm, Erwin Moller
          <Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
          >rotron schreef:
          >>
          >>
          >>
          >>Hi all,
          >>Kind of a PHP newbie and was wondering if anyone could provide some
          >>guidance. I have 2 CSV files with 2 columns each and want to find the
          >>intersectio n of both based on the value in the first column. For
          >>example
          >>File 1
          >>id1, apple
          >>id2, orange
          >>id3, banana
          >>id4, durian
          >>id5, tomato
          >>File 2
          >>id7, coke
          >>id6, drpepper
          >>id5, sprite
          >>id4, 7up
          >>id1, water
          >>My desired output is:
          >>id1, apple, water
          >>id4, durian, 7up
          >>id5, tomato, sprite
          >>I've been trying to create some PHP scripts to do that, using merge,
          >>usort, array_intersect . I think my main problem is that when use
          >>fgetcsv, I'm creating multi-dimensional arrays.
          >>I *think* what I should be doing is creating single dimension arrays
          >>with IDs as keys, and the fruit/drink as values. Then I should be able
          >>to use array_intersect _assoc to compare the keys from each list and
          >>match IDs.
          >>However, I don't know how to go from id1, apple to array(id1 =>
          >>apple). Anyone know how I can read the CSV files in this way? Am i on
          >>the right track?
          >>Thanks!
          >Hi,
          >>
          >Well, for starters, this should be done in the database.
          >Writing a query that produces excactly that is easy.
          >>
          >So before helping out with your current approach (the flat files) I have
          > a question: Can you go back to the database and solve it over there?
          >>
          >Regards,
          >Erwin Moller
          >
          Hi Erwin,
          Thanks for your reply.
          The 2 CSV files are straight from Excel. I could do a Vlookup to get
          the desired result, but am trying to automate this as much as possible.
          Hi,

          Ok, I normally don't code for free, but here we go:

          <?php
          /*
          File 1

          id1, apple
          id2, orange
          id3, banana
          id4, durian
          id5, tomato

          File 2

          id7, coke
          id6, drpepper
          id5, sprite
          id4, 7up
          id1, water

          My desired output is:

          id1, apple, water
          id4, durian, 7up
          id5, tomato, sprite
          */

          // add more if you want
          $mergeUs = array ("file1.csv" , "file2.csv" );

          // read all in 1 file:
          $allCSV = array();
          foreach ($mergeUs as $oneFile){
          $allLines = file($oneFile);
          $allCSV = array_merge($al lCSV,$allLines) ;
          }

          $result = array();
          foreach ($allCSV as $oneLine){
          // only accept lines with a , and do trimming
          $parts = explode(",",$on eLine);
          if (count($parts) == 2){
          // accept
          $key = trim($parts[0]);
          $val = trim($parts[1]);
          // add
          $result[$key][] = $val;
          }
          }

          // spit them out:
          foreach ($result as $theKey =$resArr){
          echo $theKey.", ".implode(" , ",$resArr)."\n< br>";
          }

          ?>

          Regards,
          Erwin Moller

          Comment

          • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

            #6
            Re: CSV to associative array

            rotron escribió:
            I've been trying to create some PHP scripts to do that, using merge,
            usort, array_intersect . I think my main problem is that when use
            fgetcsv, I'm creating multi-dimensional arrays.
            You paste no code at all, but from your description of your failed
            attempts it seems that you don't know what fgetcsv() returns and you
            take for granted that the first column receives a different handling and
            becomes a primary key; it doesn't:

            <?php

            $fp = fopen('1.txt', 'rt');
            $a1 = array();
            while(!feof($fp )){
            $a1[] = fgetcsv($fp);
            }
            fclose($fp);

            $fp = fopen('2.txt', 'rt');
            $a2 = array();
            while(!feof($fp )){
            $a2[] = fgetcsv($fp);
            }
            fclose($fp);

            print_r($a1);
            print_r($a2);

            ?>

            This outputs:

            Array
            (
            [0] =Array
            (
            [0] =id1
            [1] =apple
            )

            [1] =Array
            (
            [0] =id2
            [1] =orange
            )

            [2] =Array
            (
            [0] =id3
            [1] =banana
            )
            [...]
            )

            Combine both arrays with array_merge() and create a the resulting array
            within a foreach() loop.



            --
            -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
            -- Mi sitio sobre programación web: http://bits.demogracia.com
            -- Mi web de humor al baño María: http://www.demogracia.com
            --

            Comment

            Working...