PHP:将字符串转换为数组有特殊要求

时间:2017-03-01 08:34:13

标签: php

如何从文件中创建具有长字符串的数组?

我的字符串如下:

115 = c

116 = a

等...

每一行都是一样的。

我希望有这样的数组: " 115" = GT;" C" " 116" = GT;""

1 个答案:

答案 0 :(得分:0)

你的问题并不是真的可以理解,缺乏正确的格式,指向你尝试过的东西,或指出你的代码失败了。所以不是要为你写东西,而是纠正并引导你。也就是说,如果你想从文件中读取内容并使用它,你可以这样做:

// Readonly open a file, and handle it line per line
$newArray = array();
$handle = fopen("inputfile.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
        $lineExploded = explode('=', $line);
        $newArray[ trim($lineExploded[0]) ] = trim($lineExploded[1]);
    }

    fclose($handle);
} else {
    // error opening the file.
} 
相关问题