数组在PHP中消失

时间:2016-03-18 07:47:51

标签: php arrays codeigniter static

这是我的代码:

public function pgn_oku() {
    $n=-1;
    $x=-1;  
    $detay="";
    $hamle="";
    $file = fopen("D:\satranc\bielcki\kaleli_roklu_vezirli.pgn","r");

    while(! feof($file)) {
        $n=$n+1;
        $detay[$n]= fgets($file);
        $detay[$n]=trim($detay[$n]);
        if (empty($detay[$n])) {
            $x=$x+1;
            $hamle1[$x]= fgets($file);
            $hamle = explode(' ', $hamle1[$x]);
            print_r(array_values($hamle))."<br>";
        } else  {
            echo $detay[$n]."<br>";
            echo $n."<br>";
        }
    }

    print_r(array_values($detay))."<br><br>";
    echo "<br><br>";
    print_r(array_values($hamle))."<br>";
    echo "<br><br>";

    fclose($file);
}

这就是我得到的结果:

[Event "?"]
0
[Site "?"]
1
[Date "????.??.??"]
2
[Round "?"]
3
[White "?"]
4
[Black "?"]
5
[Result "*"]
6
Array ( [0] => 1.b4 [1] => c5 [2] => 2.bxc5 [3] => d6 [4] => 3.cxd6 [5] => Qxd6 [6] => 4.e3 [7] => Nf6 [8] => 5.c4 [9] => b5 [10] => 6.cxb5 [11] => e5 [12] => 7.b6 [13] => Be7 [14] => 8.bxa7 [15] => O-O [16] => 9.axb8=Q [17] => Bd7 [18] => 10.Qbb3 [19] => Rab8 [20] => 11.Qbc2 [21] => Rfd8 [22] => 12.Qe2 [23] => Rdc8 [24] => 13.Qcd3 [25] => Qe6 [26] => 14.Qf3 [27] => Qd5 [28] => 15.Qfxd5 [29] => Nxd5 [30] => 16.Qxd5 [31] => Rxc1+ [32] => 17.Ke2 [33] => Rbxb1 [34] => 18.Qxd7 [35] => Rxa1 [36] => 19.Nf3 [37] => Rxa2 [38] => 20.Qxe7 [39] => Rcc2 [40] => 21.h4 [41] => Rcb2 [42] => 22.h5 [43] => Rb8 [44] => 23.h6 [45] => Rba8 [46] => 24.Rh5 [47] => R2a7 [48] => 25.Qxe5 [49] => * ) Array ( [0] => ) Array ( [0] => [Event "?"] [1] => [Site "?"] [2] => [Date "????.??.??"] [3] => [Round "?"] [4] => [White "?"] [5] => [Black "?"] [6] => [Result "*"] [7] => [8] => )

Array ( [0] => ) 

问题是数组$hamle打印在上方但不在下方。我玩了STATIC声明,但无济于事。任何帮助将不胜感激。

注意:我正在使用CodeIgniter框架。

我正在阅读的文件是:

[Event "?"] [Site "?"] [Date "????.??.??"] [Round "?"] [White "?"] [Black "?"] [Result "*"] 1.b4 c5 2.bxc5 d6 3.cxd6 Qxd6 4.e3 Nf6 5.c4 b5 6.cxb5 e5 7.b6 Be7 8.bxa7 O-O 9.axb8=Q Bd7 10.Qbb3 Rab8 11.Qbc2 Rfd8 12.Qe2 Rdc8 13.Qcd3 Qe6 14.Qf3 Qd5 15.Qfxd5 Nxd5 16.Qxd5 Rxc1+ 17.Ke2 Rbxb1 18.Qxd7 Rxa1 19.Nf3 Rxa2 20.Qxe7 Rcc2 21.h4 Rcb2 22.h5 Rb8 23.h6 Rba8 24.Rh5 R2a7 25.Qxe5 *

1 个答案:

答案 0 :(得分:0)

我找到的答案如下=&gt;代码工作它一直到要读取的文件的末尾,并在最后一刻读取最后一行没有出现在复制和粘贴(它只是一个\ n char我想)  如下所示,技巧=&gt;

 $hamle = explode(' ', $hamle1[$x]);
    $GLOBALS['hamleg'] = $hamle;

         break;