doujianmin6527 2017-11-15 07:12
浏览 144
已采纳

将.text文件内容读入php多维数组

I have a .txt file with content like this

  1. Abuja, the Federal Capital Territory has -- -- -- -- -- area Council
    A. 4
    B. 6
    C. 7
    D. 2
    ANSWER: B

  2. The Federal Capital Territory is associated with-- -- -- -- -- -- -- vegetation belt
    A. Sahel savanna
    B. Rainforest
    C. Guinea savanna
    D. Sudan savanna
    ANSWER: C

  3. The most significant factor responsible for the ever increasing population of FCT is
    A. High birth rate
    B. Immigration
    C. Death rate
    D. CENSUS
    ANSWER: B

i would love to read the file content into a multi dimensional array so i can get each questions, its answers and the correct answer for each of the questions.

i have tried this:-

$array=explode("
", file_get_contents('file.txt')); 
print_r($array);

but it doesn't give me what i want..

  • 写回答

2条回答 默认 最新

  • douchengchu8374 2017-11-15 08:21
    关注

    Alives answer gives a result that you probably can work with, but I think associative array is probably the way to go.

    I look at each line to see if it has a question number => add new item in array with question number and question text.

    If first char is letter and second is a dot, it's an answer => add answer letter as key and text as value.

    If it's none of above it's the answer text => add key with ANSWER and value as the correct answer.

    I use explode to split the lines. The third argument tells how many parts to split the string in.
    With "2" it splits at first space meaning I have the question# as item 1 and question text as item 2 in the array.

    https://3v4l.org/ZqppN

    // $str = file_get_contents("text.txt");
    $str = "1. Abuja, the Federal Capital Territory has -- -- -- -- -- area Council
    A. 4
    B. 6
    C. 7
    D. 2
    ANSWER: B
    
    2. The Federal Capital Territory is associated with-- -- -- -- -- -- -- vegetation belt
    A. Sahel savanna
    B. Rainforest
    C. Guinea savanna
    D. Sudan savanna
    ANSWER: C
    
    3. The most significant factor responsible for the ever increasing population of FCT is
    A. High birth rate
    B. Immigration
    C. Death rate
    D. CENSUS
    ANSWER: B";
    
    $arr = explode("
    ", $str);
    
    $res=[];
    
    Foreach($arr as $line){
        If($line != ""){
            If(is_numeric($line[0])){
                Preg_match("/^\d+/", $line, $num);
                $res[$num[0]] =["QUESTION" =>explode(" ", $line,2)[1]];
                $q = $num[0];
            }Else if(ctype_alpha($line[0]) && $line[1] == "."){
                $res[$q][$line[0]] = explode(" ", $line, 2)[1];
            }Else{
                $res[$q]["ANSWER"] = trim(explode(":", $line, 2)[1]);
            }
        }
    }
    
    Var_dump($res);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line