Question

Basically, here is my CSV File:

1,"Gold"
2,"English Version"
10,"Sword+0"
11,"Sword+1"
12,"Sword+2"

And so on, you get the idea. There are other parts where the ID is not incremental, perhaps one is 2899 and then the next one is 3020. I'm trying to build an array from this with fgetcsv();. I can do it fine, but I've failed so far to match up my array IDs with the ID from the CSV.

Here's a simple one that simply builds an incremental array from the file:

$file = fopen("item_proto.csv", "r");
$i = 1;
while(! feof($file)){
  $gvar['item'][$i] = (fgetcsv($file));  
  $i++;
  }
fclose($file);

This of course results in:

Array
(
    [item] => Array
        (
            [1] => Array
                (
                    [0] => 1
                    [1] => Gold
                )

            [2] => Array
                (
                    [0] => 2
                    [1] => English Version
                )

            [3] => Array
                (
                    [0] => 10
                    [1] => Sword+0

But I'd like [item][x] to match up with [item][x][y].

Was it helpful?

Solution

Try this:

$file = fopen("item_proto.csv", "r");
$i = 1;
while(! feof($file)){
  $line = fgetcsv($file);
  $gvar['item'][$line[0]] = $line;
  $i++;
  }
fclose($file);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top