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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.