Ver Mensaje Individual
  #2 (permalink)  
Antiguo 04/03/2014, 14:31
striderwar
 
Fecha de Ingreso: diciembre-2005
Ubicación: Jalisco
Mensajes: 31
Antigüedad: 19 años, 3 meses
Puntos: 0
Respuesta: leer csv y guardar los headers

Por si a alguien le sirve...

Código PHP:
<?php

function csv_in_array($url,$delm=",",$encl="\"",$head=true) {
   
    
$csvxrow file($url);   // ---- csv rows to array ----
    
$doc DOMDocument::loadHTML("LISTOTA.csv");
    
    
$csvxrow[0] = chop($csvxrow[0]);
    
$csvxrow[0] = str_replace($encl,'',$csvxrow[0]);
    
$keydata explode($delm,$csvxrow[0]);
    
$keynumb count($keydata);
    
    if (
$head === true) {
    
$anzdata count($csvxrow);
    
$z=0;
    
$h=0;
    
$j=0;
    for(
$x=1$x<$anzdata$x++) {
        
$csvxrow[$x] = chop($csvxrow[$x]);
        
$csvxrow[$x] = str_replace($encl,'',$csvxrow[$x]);
        
$csv_data[$x] = explode($delm,$csvxrow[$x]);
        
$i=0;
        foreach(
$keydata as $key) {
            
$out[$z][$key] = $csv_data[$x][$i];
            
$i++;
            }
        
$z++;
        }
         
    }
    else {
        
$i=0;
        foreach(
$csvxrow as $item) {
            
$item chop($item);
            
$item str_replace($encl,'',$item);
            
$csv_data explode($delm,$item);
            for (
$y=0$y<$keynumb$y++) {
               
$out[$i][$y] = $csv_data[$y];
            }
        
$i++;
        }
    }

return 
$out;
}

// --------------------------------------------------------------

?>

<?php
/*fuction call with 4 parameters:

(1) = the file with CSV data (url / string)
(2) = colum delimiter (e.g: ; or | or , ...)
(3) = values enclosed by (e.g: ' or " or ^ or ...)
(4) = with or without 1st row = head (true/false)
*/
?>

<?php

// ----- call ------
$csvdata csv_in_array("LISTOTA.csv"",""\""true);
// -----------------

// ----- view ------
echo "<pre>\r\n";
print_r($csvdata);
echo 
"</pre>\r\n";
// -----------------
?>