1

I have a CSV file with 8 column and i want to store any column into an array and working with them.

Example:Num,UnitId,SubscriberId,ActivationType,StartTime,EndTime,LicenseCount,ProductId

how can i do this??

php code:

class CSVparse
  {
  var $mappings = array();
  function parse_file($filename)
    {
    $id = fopen($filename, "r");
    $data = fgetcsv($id, filesize($filename));
    if(!$this->mappings)
       $this->mappings = $data;
    while($data = fgetcsv($id, filesize($filename)))
        {
         if($data[0])
           {
            foreach($data as $key => $value)
               $converted_data[$this->mappings[$key]] = addslashes($value);
            $table[] = $converted_data;
             }
         }
    fclose($id);
    print_r($table);
    }
  }
$csv = new CSVparse;
$csv->parse_file("sample.csv");

output:

Array ( [0] => Array ( [Num] => 1 [UnitID] => 1 [SubscriberId] => 111 [ActivationType] => Standard [StartTime] => 8/5/2015 11:16 [EndTime] => 2015-09-05T11:16:00.7514332+04:30 [LicenseCount] => 2 [ProductId] => KISA1 ) [1] => Array ( [Num] => 2 [UnitID] => 1 [SubscriberId] => 222 [ActivationType] => Standard [StartTime] => 8/5/2015 11:16 [EndTime] => 2015-09-05T11:16:00.7514332+04:30 [LicenseCount] => 34 [ProductId] => KISA3 ) [2] => Array ( [Num] => 3 [UnitID] => 1 [SubscriberId] => 333 [ActivationType] => Standard [StartTime] => 8/5/2015 11:17 [EndTime] => 2015-09-05T11:17:27.6310205+04:30 [LicenseCount] => 12 [ProductId] => KISA6 ) [3] => Array ( [Num] => 4 [UnitID] => 1 [SubscriberId] => 444 [ActivationType] => Standard [StartTime] => 8/5/2015 11:17 [EndTime] => 2015-09-05T11:17:27.6310205+04:30 [LicenseCount] => 33 [ProductId] => KISA12 ) ) 

1 Answer 1

1

You can pass a field name to the function and do a check on that field it is set.

class CSVparse
  {
  var $mappings = array();
  function parse_file($filename, $fieldname = false)
    {
    $id = fopen($filename, "r");
    $hdata = fgetcsv($id, filesize($filename));
    if(!$this->mappings)
       $this->mappings = $hdata;
    while($data = fgetcsv($id, filesize($filename)))
    {
        foreach($data as $key => $value) {
            if ($fieldname) if ($this->mappings[$key] != $fieldname) continue;
            $converted_data[$this->mappings[$key]] = addslashes($value);
        }
        $table[] = $converted_data;
    }
    fclose($id);
    print_r($table);
    }
  }
$csv = new CSVparse;
$csv->parse_file("sample.csv", 'UnitID');

To make it more usable you could make it an array of fields and do an array_search() in the conditional.

Sign up to request clarification or add additional context in comments.

2 Comments

the output of your code will return this for all column:Array ( [0] => Array ( [Num] => 1 ) [1] => Array ( [Num] => 2 ) [2] => Array ( [Num] => 3 ) [3] => Array ( [Num] => 4 ) )
Edited to fix error - the key needs to be mapped to the field name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.