douzhantanju1849 2017-09-01 07:39
浏览 60

从文件读取到关联数组

I have to create a page, where the user is able to search a suburb and the page will print the postcode of that suburb.

I am having a little difficulty with putting the data from the .txt document into the variables for the associative array.

Thanks for your help.

This is what I have so far.

    <?php

    $file = "postcode.txt";
    $handle = fopen($file, 'r');
    $postcodearray = file($file);

    $suburb = explode(",", );
    $postcodearray[$suburb] = $postcode;

    fclose($handle)
    ?>

and this is the format of the .txt document...

3000,MELBOURNE  
3001,MELBOURNE  
3002,EAST MELBOURNE  
3003,WEST MELBOURNE  

etc.

  • 写回答

3条回答 默认 最新

  • doujiao8649 2017-09-01 07:48
    关注

    I prefer file_get_contents when working with files

    <?php
    
    $content = file_get_contents('postcode.txt');
    
    $rows = explode("
    ", $content);
    
    $data = [];
    foreach ($rows as $row) {
        $columns = explode(',', $row);
    
        $data[$columns[0]] = $columns[1];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?