在PHP中将顶级值更改为2d数组之一

时间:2013-12-19 15:28:05

标签: php arrays csv

在我的代码中,我访问了一个按顺序排序的csv;

0       BrkesGdTiresFair
0       ChessDarts
0       TrainerGeddon
50      EnoughsEnough
100     ShortButFun
101     GrayGlitter
499     PowerAnimal
500     FakeFake
3046    DinosrStlThr
5048    GnrtnlAmnsia
5048    NotEvenWrong
10000   MosquitoSwamp
10048   DrSatansRobot
22042   Kittenszzz

并制作一个2d数组;

  if(($handle = fopen("voting.csv", 'r')) !== FALSE) {
        set_time_limit(0);
        while(($data = fgetcsv($handle,0,',')) !== FALSE) {
          $ListV[$row]=$data;
          $row++;                   
        }
      fclose($handle);
     }

我想要一个能够将列表顶部项目的得分改为1的函数。 所以......

   1    BrkesGdTiresFair

我以为会这样做;

   $newScore =int($ListV[0][0]);
   $ListV[0][0]=$newScore+1;

但它没有做任何事。我对此进行沙盒处理并取得了良好的效果;

$array = array();

$array[] = array(0, 0, 0, 0);
$array[] = array(5, 5, 5, 5);

$array[0][0]++;

echo $array[0][0]

1

试过这个......

   $ListV[0][0]++;

刚刚得到......

0       BrkesGdTiresFair
0       ChessDarts
0       TrainerGeddon
50      EnoughsEnough
100     ShortButFun
101     GrayGlitter
499     PowerAnimal
500     FakeFake
3046    DinosrStlThr
5048    GnrtnlAmnsia
5048    NotEvenWrong
10000   MosquitoSwamp
10048   DrSatansRobot
22042   Kittenszzz
1

是我写它的方式吗?

   $listnewV=fopen("ListV.csv",'w');
   foreach ($ListV as $values) {fputcsv($listnewV,$values);}
   fclose ($listnewV);

1 个答案:

答案 0 :(得分:1)

count   name
0       BrkesGdTiresFair
0       ChessDarts
0       TrainerGeddon
50      EnoughsEnough
100     ShortButFun
101     GrayGlitter
499     PowerAnimal
500     FakeFake
3046    DinosrStlThr
5048    GnrtnlAmnsia
5048    NotEvenWrong
10000   MosquitoSwamp
10048   DrSatansRobot
22042   Kittenszzz


$listV = loadCSV('voting.csv');
$listV[0]['count'] += 1;

$listNewV = fopen("ListV.csv", 'w');
foreach ($ListV as $values) {
    fputcsv($listNewV, $values, "\t");
}
fclose($listNewV);


function loadCSV($file)
{
    $handle = fopen($file, 'r');
    $header = fgetcsv($handle, 10000, "\t");
    $data = array();
    while ($row = fgetcsv($handle, 10000, "\t")) {

        $row_assoc = array();
        foreach ($row as $key => $field) {
            $row_assoc[$header[$key]] = $field;
        }
        $data[] = $row_assoc;
    }
    fclose($handle);
    return $data;
}