2010-04-23 19 views
1

基本上大廈ID'd陣列,是我的CSV文件:PHP,非增量ID'd CSV

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

等等,你的想法。還有其他地方的ID不是增量的,也許一個是2899,然後下一個是3020.我試圖用fgetcsv();從這個數組構建一個數組。我可以做得很好,但是我迄今爲止沒有把我的陣列ID與來自CSV的ID匹配起來。

這裏有一個簡單的,簡單地構建從文件的增量陣列:

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

當然結果此:

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

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

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

但我想[項目] [×]與[item] [x] [y]匹配。

回答

1

試試這個:

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

謝謝!雖然我想現在在結果數組中,我不需要包含相同值的冗餘[0]。我不知道如何使用它來生成父母的ID後才能刪除它。 – 2010-04-23 15:18:53

+0

明白了; 未設置($ gvar ['item'] [$ line [0]] [0]); 在while循環中。再次感謝! – 2010-04-23 15:24:45