Your Ad Here

Posted By

nhassan on 10/31/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


csv to array


 / Published in: PHP
 

  1. <?php
  2. function csv_explode( $file ) {
  3. $lines = explode("\n", $file);
  4.  
  5. $use_first_row_as_key = true;
  6.  
  7. $final_output = array();
  8. foreach( $lines as $line ) {
  9. $line = explode(',', $line);
  10.  
  11. if( is_bool($use_first_row_as_key) && $use_first_row_as_key == true ) {
  12. $use_first_row_as_key = $line;
  13. } else {
  14.  
  15. if( is_array($use_first_row_as_key) ) {
  16. foreach( $line as $key => $value ) {
  17. unset( $line[$key] );
  18. $line[ strtolower($use_first_row_as_key[$key]) ] = $value;
  19. }
  20. }
  21.  
  22. $final_output[] = $line;
  23. }
  24. }
  25.  
  26. return $final_output;
  27. }
  28. ?>

Report this snippet  

You need to login to post a comment.