2015-09-23 22:25

如何将文件转换为关联数组? 并使用前3个字符作为键

  • file
  • php
  • arrays

I have a file like below, which I need to convert into an array:

ABCLine, Number, One
DEFNumber, Two, Line
GHIThree, Line, Number

I can get each line and turn it into a value of an array, but what I need to do is take the first 3 characters and turn that into a key and then the rest of the line into the value.

So my expected array would be:

Keys  |      Values
ABC   |  Line, Number, One
DEF   |  Number, Two, Line
GHI   |  Three, Line, Number

I honestly am not too sure where to begin, I've been looking all over and haven't been able to find a way to just take those first 3 characters and turn those into a key for the remainder of the line.

I started with some code, which looks like this:


  echo "Name<br/>";

  $file = "hw3.txt";
  $f1 = fopen($file, 'r');
  $array = array();

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享