doukanmang3687 2016-05-10 02:46
浏览 142
已采纳

如果数组为空,如何删除它的最后一个元素

Below is the print_r version of my array ($hamle). As you can see index(80) is empty or there is a end of line or a similar character there. I tried many options to delete it if it exists and is empty but couldn't do it. I strongly think that it is reading an empty line during the 'read while' loop. It doesn't appear in all files. It appears sometimes. Can anyone think of a chic way out?

Array ( [0] => 1.b4 [1] => d5 [2] => 2.Bb2 [3] => Qd6 [4] => 3.a3 [5] => e5 [6] => 4.e3 [7] => a5 [8] => 5.b5 [9] => Nf6 [10] => 6.c4 [11] => Bg4 [12] => 7.Be2 [13] => Bxe2 [14] => 8.Qxe2 [15] => Nbd7 [16] => 9.d4 [17] => dxc4 [18] => 10.Nf3 [19] => e4 [20] => 11.Ne5 [21] => Nb6 [22] => 12.Nd2 [23] => Qe6 [24] => 13.O-O [25] => Bd6 [26] => 14.Nexc4 [27] => Nxc4 [28] => 15.Nxc4 [29] => O-O [30] => 16.Rfc1 [31] => Be7 [32] => 17.a4 [33] => Nd5 [34] => 18.Ba3 [35] => Bb4 [36] => 19.Qb2 [37] => Rfc8 [38] => 20.Bxb4 [39] => axb4 [40] => 21.Nd2 [41] => c6 [42] => 22.b6 [43] => Qe7 [44] => 23.Rc5 [45] => Ra6 [46] => 24.a5 [47] => g6 [48] => 25.Nb3 [49] => Rca8 [50] => 26.Rac1 [51] => Kg7 [52] => 27.Qd2 [53] => Qd6 [54] => 28.R1c4 [55] => Rd8 [56] => 29.Rxb4 [57] => Nxb4 [58] => 30.Qxb4 [59] => Qe7 [60] => 31.h3 [61] => Rd5 [62] => 32.Kf1 [63] => Ra8 [64] => 33.Qa4 [65] => Qd6 [66] => 34.Ke2 [67] => Kf8 [68] => 35.Rxd5 [69] => cxd5 [70] => 36.Nc5 [71] => Ke7 [72] => 37.Qb5 [73] => Rb8 [74] => 38.Kd2 [75] => f5 [76] => 39.Kc3 [77] => g5 [78] => 40.Kb4 [79] => 1-0 [80] => )

The code =>

while(! feof($file))
  {


 $n=$n+1;
            $hamle1= fgets($file);
    $hamle1 = str_replace("
", "", $hamle1);
    $hamle1 = str_replace("", "", $hamle1);
    $hamle1 = trim($hamle1);
    $hamle1 = explode(" ", $hamle1);

        foreach($hamle1 as $item)
    {
        $hamle[] = $item;
    }
  • 写回答

2条回答 默认 最新

  • douye8500 2016-05-10 03:22
    关注

    array_filter will remove empty entries

    $hamle = array_filter($hamle);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?