views:

20

answers:

1

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].

A: 

Try this:

$file = fopen("item_proto.csv", "r");
$i = 1;
while(! feof($file)){
  $line = fgetcsv($file);
  $gvar['item'][$line[0]] = $line;
  $i++;
  }
fclose($file);
Eric Petroelje
Thanks! Though I guess now in the resulting array I don't need the redundant [0] containing the same value. I'm not sure how to remove it only after using it to generate the parent's ID though.
Jim T
Got it; unset($gvar['item'][$line[0]][0]); During the while loop. Thanks again!
Jim T