Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to parse a CSV data into 2d array. In my csv file the headings are as follows, follow by specific values as column.

CaseNumber IncidentDate IncidentTime Location Incident Disposition 10120323 3/21/2009 .... ........ ...... ......
.....

I manage to copy all the CSV file data into an array called, array And in myarray, I have created a 2d-array with only two headers array[0] = (

[0]=> 'IncidentData',

[1]=> 'Disposition')

So my final myarray should be after matching the head in array of CSV and the head in myarray and get the all column values of 'IncidentData' and 'Disposition'

Any ideas?

Here is what I have so far.

<?php
 //# Open the File.
         if (($handle = fopen("gatech.csv", "r")) !== FALSE) {
             // Set the parent multidimensional array key to 0.
             $nn = 0;
             while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
                 //# Count the total keys in the row.
                 $c = count($data);
             // Populate the multidimensional array.
                 for ($x=0;$x<$c;$x++)
                 {
                     $csvarray[$nn][$x] = $data[$x];
                 }
                 $nn++;
             }
             // Close the File.
             fclose($handle);
         }

        // take the row'ified data and columnize the array
         function columnizeArray($csvarray) {
             $array = array();
             foreach($csvarray as $key=>$value) {
                 // reparse into useful array data.
                 if ($key == 0) {
                     foreach ($value AS $key2=>$value2) {
                         $array[$key2] = array();
                         $array[$key2][] = $value2;
                     }
                 }else if ($key > 0){
                     foreach ($value as $key3=>$value3) {
                         $array[$key3][] = $value3;
                     }
                 }else{
                 }
         }
             //print_r($array[1][1]);
             return $array;
         }

        function groupColumns($array = null){
            $myarray = array(); //create myarray with sample headers
            $myarray = array(array("CaseNumber", "IncidentDate", "IncidentTime", "Location", "Incident", "Disposition"  ));
            //k = number of columns 0-5   starts k=1 so k-1 
            foreach($array as $k=>$v){ //make col into row
                for($i==0;$i<$k-1; $i++){ 
                    print_r($v); //test: prints everything on CSV the columns as row
                    while($array[0][$i] != $myarray[0][$i])
                        if($array[0][$k] == $myarray[0][$k])
                            print_r($myarray[0][$k]);
                        else
                            print_r('test : not reading');
                }
            }
        }
         $array2 = groupColumns(columnizeArray($csvarray));
?>
share|improve this question
    
Not entirely sure what you mean by this question - would you be able to show us a few rows of the csv data that you have; the result you're currently getting; and the result you hope to achieve. –  Mikey Apr 17 '12 at 18:09
    
here is some outputs. I manage to print out by columns. Array ( [0] => CaseNumber [1] => 12030537 [2] => 2012027510 [3] => 12030539 [4] => 12030541 [5] => 12030540 [6] => 12030542 [7] => 12030543 [8] => 12030544 [9] => 12030545 [10] => 2012027399 [11] => 12030538 [12] => 12030532 [13] => 12030546 [14] => 12030548 [15] => 12030550 [16] => 12030551 [17] => 12030552 [18] => 12030549 [19] => 12030547 [20] => 12030553 [21] => 12030558 [22] => 2012027994 [23] => 12030554 [24] => 12030559 [25] => 12030560 [26] => 2012028265 [27] => 12030555 [28] => 12030557 [29] => 12030561 [30] => 120305) –  user28699 Apr 17 '12 at 18:15
    
and as follows, Array ( [0] => Location [1] => Noble Creek Drive [2] => Atlantic Drive/Area 4 Visitors Lot [3] => North Avenue Apartments [4] => Perry-Matheson Residence Hall [5] => Clough Undergraduate Learning Commons [6] => Ferst Center for the Arts [7] => Perry-Matheson Residence Hall [8] => Clough Undergraduate Learning Center (CULC) [9] => Ferst Drive @ Fowler Street –  user28699 Apr 17 '12 at 18:17
    
To make it clear, I would like to match the column headings of array with myarray headings if exists, copy the column into myarray. –  user28699 Apr 17 '12 at 18:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.