从文本文件读取二维数组,帮助!

发布于 2024-11-09 10:01:25 字数 831 浏览 0 评论 0原文

更新:我意识到问题是我使用 print 而不是 echo 来打印数据,因此它显示的是数组而不是其中的数据。非常感谢大家!

我目前有一个如下所示的文本文件:

0,0,0,0,0
0,0,0,0,0
0,0,0,0,0
0,0,0,0,0
0,0,0,0,0

我正在使用这个函数:

function rFile($fileName){
$resultF = fopen($fileName, "r") or die("can't open file");
$array = array(); //Create the first dimension of a 2D array
$i=0;
while(!feof($resultF)){
  $line = fgets($resultF);
  $line = trim($line, "\n");
  $tokens = explode(",",$line);
  $array[$i]=array(); //Create the second dimension of the 2D array
  $tokenCount = sizeof($tokens);
  for($j=0; $j<$tokenCount; $j++){
    $array[$i][$j] = $tokens[$j];
  }
  $i++;
 }
return $array;
  }

本质上,它应该读取该文件,分解每个“0”并将其存储在二维数组 $array 中。由于某种原因它返回:

Array[0]
Array[1]
Array[2]
....etc etc

有人知道我做错了什么吗?

UPDATE: I realized that the issue was I was using print instead of echo to print the data, so it was showing the array instead of the data within it. Thanks a ton guys!

I currently have a text file that looks like this:

0,0,0,0,0
0,0,0,0,0
0,0,0,0,0
0,0,0,0,0
0,0,0,0,0

And I'm using this function:

function rFile($fileName){
$resultF = fopen($fileName, "r") or die("can't open file");
$array = array(); //Create the first dimension of a 2D array
$i=0;
while(!feof($resultF)){
  $line = fgets($resultF);
  $line = trim($line, "\n");
  $tokens = explode(",",$line);
  $array[$i]=array(); //Create the second dimension of the 2D array
  $tokenCount = sizeof($tokens);
  for($j=0; $j<$tokenCount; $j++){
    $array[$i][$j] = $tokens[$j];
  }
  $i++;
 }
return $array;
  }

Essentially, it's supposed to read through the file, explode each "0" and store it in a 2D array, $array. For some reason it returns this:

Array[0]
Array[1]
Array[2]
....etc etc

Anyone know what I did wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

手长情犹 2024-11-16 10:01:25

PHP 多维数组只是数组的数组。不需要内循环。您只需这样做即可

while(...) {
   ... fgets stuff
   $array[$i] = explode(',', $line);
   $i++;
}

获得相同的效果。

PHP multi-dimensional arrays are just arrays of arrays. There's no need for the inner loop. You can just do

while(...) {
   ... fgets stuff
   $array[$i] = explode(',', $line);
   $i++;
}

and get the same effect.

御弟哥哥 2024-11-16 10:01:25

您将通过使用 for 循环和计数器来以困难的方式解决这个问题。通过使用 PHP 的 $array[] = $val 附加语法,您可以在这里节省大量精力。

// all the file handling code...

while(!feof($resultF)){
  $line = fgets($resultF);
  $line = trim($line, "\n");
  $tokens = explode(",",$line);

  // Append the line array.
  $array[] = $tokens; //Create the second dimension of the 2D array
}

return $array;

或者更简洁:

$array[] = explode(",",$line);

You're going about it the hard way, by using for loops and counters. By using PHP's $array[] = $val append syntax, you can save a lot of effort here.

// all the file handling code...

while(!feof($resultF)){
  $line = fgets($resultF);
  $line = trim($line, "\n");
  $tokens = explode(",",$line);

  // Append the line array.
  $array[] = $tokens; //Create the second dimension of the 2D array
}

return $array;

Or to be even more concise:

$array[] = explode(",",$line);
悲念泪 2024-11-16 10:01:25

试试这个:

function rFile($filename) {
    $lines = file($filename);

    if ($lines !== false) {
        foreach ($lines as & $line) {
            $line = explode(',', trim($line, '\n\r'));           
        }
    }

    return $lines;
}

try this :

function rFile($filename) {
    $lines = file($filename);

    if ($lines !== false) {
        foreach ($lines as & $line) {
            $line = explode(',', trim($line, '\n\r'));           
        }
    }

    return $lines;
}
余罪 2024-11-16 10:01:25
$f = file($fileName);

$results = array();

foreach ($f as $line) {
    $line = preg_replace("\n", "", $line);
    $results[] = explode(",", $line);
}
$f = file($fileName);

$results = array();

foreach ($f as $line) {
    $line = preg_replace("\n", "", $line);
    $results[] = explode(",", $line);
}
琴流音 2024-11-16 10:01:25

嗯,由于这是逗号分隔的,我们可以使用 fgetcsv 来使这个简短而简单:

$file = fopen('test.txt', 'r');

$matrix = array();
while($entries = fgetcsv($file)) {
  $matrix[] = $entries;
}

fclose($file);

结果数组:

Array
(
    [0] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [1] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [2] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [3] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [4] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

)

Hmm, since this is comma delimitated, we can use fgetcsv to make this short and simple:

$file = fopen('test.txt', 'r');

$matrix = array();
while($entries = fgetcsv($file)) {
  $matrix[] = $entries;
}

fclose($file);

Resulting array:

Array
(
    [0] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [1] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [2] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [3] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [4] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文