convert file content to array

1

I'm having problems when I convert a csv file to an array. You see, I created a function that makes that conversion and returns the array. The process does it relatively "well" and it generates the array but fix as it is the resulting array:

  • I'll show the function first:

      function convertions($file, $delimt, $encl, $a) { 
    
               if(!file_exists($file)){ 
                  return false; 
                                }
    
    
                $file_lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    
    
               if($file_lines === array()){
               return NULL;
                          }
    
    
                                    if($a === true) {
    
                  $file_row = array_shift($file_lines);
                  $headers = array_map('trim', str_getcsv($file_row, $delimt, $encl));
                          }
    
                    $array_result = NULL; 
    
    
                      foreach ($file_lines as $row) {
    
                       if(trim($row) === '')
                                  continue; 
    
    
                               $fields_arr = array_map('trim', str_getcsv($row, $delimt, $encl));
    
    
                      if($a === true) { 
                              $array_result[] = array_combine ($headers, $fields_arr); 
    
                                      }
    
                                 else {
                                 $array_result[] = $fields_arr; 
                                   }
                                 }
    
                              return $array_result; 
                                  }
                        $new_array = array();
                        $new_array = convertions('file.csv',',','"',true);
                        print_r($new_array);
    
  • I will post the content of my file "file.csv" here:

       code,name,type,years,versions,rivals,Cost,started,Till
       ookh6547,JAVA,language,20,19874654,C#,0,09/12/1990,present
       25aookh6547,C#,language,30,963542432,JAVA,0,10/10/1989,present
       9ddc3552,PHP,language,25,4543664,python,0,13/5/1994,present
    
  • file photo:

  • This is the result when I print the array:

      Array ( [0] => Array ( [��code] => ookh6547 [name] => JAVA [type] => language [years] => 20 [versions] => 19874654 [rivals] => C# [Cost] => 0 [started] => 09/12/1990 [till] => present ) [1] => Array ( [��code] => 25aookh6547 [name] => c# [type] => language [years] => 30 [versions] => 963542432 [rivals] => JAVA [Cost] => 0 [started] => 10/10/1989 [Till] => present ) [2] => Array ( [��code] => 9ddc3552 [name] => PHP [type] => language [years] => 25 [versions] => 4543664 [rivals] => python [Cost] => 0 [started] => 13/5/1994 [Till] => present ) )
    
  • When I try to do this for example:

        echo $new_array[0]['type'];
    

    shows me this error:

          Notice: Undefined index: type in C:\xampp\htdocs\ArraysMult\proof.php on line 88
    

    I do not understand what's wrong with the headers, it's like I do not recognize them, or I do not know. For example, also do another function (let's call it fun2) and introduce as parameter this array generated by the function convertions (), and also pass as a second parameter a variable with the name of one of the headers, example $ header="type" ; fun2 (array1, $ header); and I had the function look inside the array to see if one of the keys had the same name, and I had to return "YES" or "NO", depending on what I found. Well, it always throws me "NO", and it's wrong because inside the arrangement if that key (type) is present. I do not understand what happens with these csv files.

    What should stay is something like this:

        Array ( [0] => Array ( [code] => ookh6547 [name] => JAVA [type] => language [years] => 20 [versions] => 19874654 [rivals] => C# [Cost] => 0 [started] => 09/12/1990 [Till] => present ) [1] => Array ( [code] => 25aookh6547 [name] => C# [type] => language [years] => 30 [versions] => 963542432 [rivals] => JAVA [Cost] => 0 [started] => 10/10/1989 [Till] => present ) [2] => Array ( [code] => 9ddc3552 [name] => PHP [type] => language [years] => 25 [versions] => 4543664 [rivals] => python [Cost] => 0 [started] => 13/5/1994 [Till] => present ) )
    

    Is there any way to solve this problem? Does PHP have any way of "formatting" the csv file, or, failing that, format the data extracted from that file before doing the array conversion? That problem has kept me stuck all day.

    I hope you can give me any suggestions or examples that will help me solve this problem.

        
    asked by RolandF 07.04.2018 в 04:47
    source

    1 answer

    0

    I just made a show, try it:

    function parseCsv( string $csvFile, bool $stripUnicode = false ) {
        if(pathinfo($csvFile, 4)=='csv'){
            $pattern = $stripUnicode ? "/(^[ \t]+|[ \t]+$)|\p{So}/u" : "/^[ \t]+|[ \t]+$/";
            $csvFile = array_map(function($e) use ($stripUnicode, $pattern){
                static $c = 0; 
                static $headers = [];
                $s = $c == 0 ? 'headers' : 'record';
                $$s = preg_replace($pattern, '', explode(',', $e));
                $c++;
                return isset($record) ? @array_combine($headers, $record) : false;
            }, file($csvFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
            return array_values(array_filter($csvFile));
        } else { return false; }
    }
    

    You only have to indicate in the first parameter where the CSV file is located. The second parameter is to remove those symbols and must be in true for it. Usually the first row of CSVs are headers. It only works with simple CSVs like the one you present.

    PS: You must store the array in a variable. Ex: $array = parseCsv('../file.csv', true);

        
    answered by 07.04.2018 в 14:15