Jump to content

Need help - fast!!


esbenp

Recommended Posts

Hey Guys,

i have a project to deliver in like 3 hours,

i found a function on php.net to parse a csv file into an array, and made my code after that.

The problem is, this function uses str_getcsv(), which is only available in 5.3, but my clients stupid server is 5.2.10 (talking about bad luck :()

 

function csv_to_array($csv, $delimiter = ',', $enclosure = '"', $escape = '\\', $terminator = "\n") {
    $r = array();
    $rows = explode($terminator,trim($csv));
    $names = array_shift($rows);
    $names = str_getcsv($names,$delimiter,$enclosure,$escape);
    $nc = count($names);
    foreach ($rows as $row) {
        if (trim($row)) {
            $values = str_getcsv($row,$delimiter,$enclosure,$escape);
            if (!$values) $values = array_fill(0,$nc,null);
            $r[] = array_combine($names,$values);
        }
    }
    return $r;
}

 

this is the function i used, can anybody please show me what to edit to make

it functional in 5.2.10 ???

 

thx :)

 

Link to comment
https://forums.phpfreaks.com/topic/194143-need-help-fast/
Share on other sites

Haven't test it and dont know what str_getcsv output but i think you can get it into array by reading it line by line

 

$fd = fopen ("file.csv", "r");

// read csv line by line

$line = 1;

$content = array();

while (!feof ($fd)) 

{

  $buffer = fgets($fd, 4096);

  $content[$line] = explode(',', $buffer);

  $i++;

}

 

Content of CSV file should be in $content

Link to comment
https://forums.phpfreaks.com/topic/194143-need-help-fast/#findComment-1021492
Share on other sites

It didn't output anything, i've got some other code here, which

does the trick, it generates this array:

 

array
  'ARAGR' => string '200' (length=3)
  'ARANR' => string '80' (length=2)
  'Bezeichnung' => string '195/70 R 15' (length=11)
  'Hersteller' => string 'FIRESTONE' (length=9)
  'Profil' => string 'CVW3000' (length=7)
  'Demo' => string '$' (length=1)
  'Zusatzbezeichnung' => string '' (length=0)
  'Tragfaehigkeit' => string '104/102' (length=7)
  'verfLB' => string '128' (length=3)
  'aktLB' => string '128' (length=3)
  'VK8' => string '465.87' (length=6)
  'Breite' => string '195' (length=3)
  'ARSQU' => string '70' (length=2)
  'ARSGI' => string 'R' (length=1)
  'ARSZG' => string '15' (length=2)
  'ARTID' => string '200' (length=3)
  'ARRART' => string '8' (length=1)
  'AREAN' => string '3286347597015' (length=13)
  'ARHKE' => string '.00' (length=3)
  'ARHANR' => string '75970' (length=5)
  'ARGEW' => string '11' (length=2)

 

function csv_to_array($csv, $delimiter = ',', $enclosure = '"', $escape = '\\', $terminator = "\n") {
    $handle = fopen($csv, 'r');
    if ($handle)
    {
        set_time_limit(0);

        //the top line is the field names
        $fields = fgetcsv($handle, 4096, ',');

        //loop through one row at a time
        while (($data = fgetcsv($handle)) !== FALSE)
        {
            $r = array_combine($fields, $data);
        }

        fclose($handle);
    }
    return $r;
}

 

but it takes one row :(

any ideas how to make it fill the array with the rest?

 

Link to comment
https://forums.phpfreaks.com/topic/194143-need-help-fast/#findComment-1021498
Share on other sites

nevermind, got it working with

 

function csv_to_array($csv, $delimiter = ',', $enclosure = '"', $escape = '\\', $terminator = "\n") {
    $handle = fopen($csv, 'r');
    if ($handle)
    {
        set_time_limit(0);

        //the top line is the field names
        $fields = fgetcsv($handle, 4096, ',');

        //loop through one row at a time
        $i = 0;
        while (($data = fgetcsv($handle)) !== FALSE)
        {
            $r[$i] = array_combine($fields, $data);
            $i++;
        }

        fclose($handle);
    }
    return $r;
}

 

:D:D:D

Link to comment
https://forums.phpfreaks.com/topic/194143-need-help-fast/#findComment-1021506
Share on other sites

Actually it works fine, I just mistake the $line for $i

 

$fd = fopen ("file.csv", "r");

// read csv line by line

$line = 1;

$content = array();

while (!feof ($fd))

{

  $buffer = fgets($fd, 4096);

  $content[$line] = explode(',', $buffer);

  $line++;  // i mistyped this

}

 

print_r($content);

Link to comment
https://forums.phpfreaks.com/topic/194143-need-help-fast/#findComment-1021760
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.