简体   繁体   English

将数据从文本文件导入数组并在PHP中进行绘制

[英]Getting Data from text file into array and plotting it in PHP

I am trying to read Data from a text file and then plot it usint PHPplot. 我正在尝试从文本文件中读取数据,然后使用usint PHPplot将其绘制。 Text file looks like this: 文本文件如下所示:

0   24
1   28
2   30
3   35
4   40

I am trying to convert the data into something like: 我正在尝试将数据转换为类似的内容:

array(array(0,24),array(1,28),array(2,30),array(3,35),array(4,40))

my code in php is like this 我在php中的代码是这样的

 $file = fopen("data2.txt", "r");;
            while (!feof($file)) {
                $line_of_text .= fgets($file);
            }
            $members = explode("\n", $line_of_text);
            fclose($file);

for ($j=0; $j<=10; $j++)
  {       
  $parts[$j]=explode("   ", $members[$j]);       
  }
# plot 
require_once 'phplot.php';
    for ($x = 0; $x <= 5; $x += 1)
      $data[] = array('', $parts[$x][0], $parts[$x][1]);
    $plot = new PHPlot(800, 600);
    $plot->SetPrintImage(False); // No automatic output
    $plot->SetImageBorderType('plain');
    $plot->SetPlotType('lines');
    $plot->SetDataType('data-data');
    $plot->SetDataValues($data);
    $plot->SetPlotAreaWorld(0, 0, 10, 40);
    $plot->SetDrawYGrid(True);
    $plot->DrawGraph();

the problem is in line: 问题出在下面:

$data[] = array('', $parts[$x][0], $parts[$x][1]);

that it does not draw the $parts[$x][1] numbers. 它不会绘制$ parts [$ x] [1]数字。 when I say print $parts[$x][1] values it prints it on the browser but just do not plot it. 当我说print $ parts [$ x] [1]值时,它会在浏览器中打印出来,但不要绘制它。 the interesting part is that when I ask it to plot 有趣的是,当我要求绘制时

$data[] = array('', $parts[$x][0], $parts[$x][1]);

It Plots this time!! 这次绘图!!

var_dump($parts) gave: var_dump($ parts)给出了:

array(11) { [0]=> array(2) { [0]=> string(1) "0" [1]=> string(3) "24 " } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(3) "28 " } [2]=> array(2) { [0]=> string(1) "2" [1]=> string(3) "30 " } [3]=> array(2) { [0]=> string(1) "3" [1]=> string(3) "35 " } [4]=> array(2) { [0]=> string(1) "4" [1]=> string(2) "40" } [5]=> array(1) { [0]=> string(0) "" } [6]=> array(1) { [0]=> string(0) "" } [7]=> array(1) { [0]=> string(0) "" } [8]=> array(1) { [0]=> string(0) "" } [9]=> array(1) { [0]=> string(0) "" } [10]=> array(1) { [0]=> string(0) "" } }

also var_dump($data) gave: var_dump($ data)也给出了:

array(5) { [0]=> array(3) { [0]=> string(0) "" [1]=> int(0) [2]=> string(3) "24 " } [1]=> array(3) { [0]=> string(0) "" [1]=> int(1) [2]=> string(3) "28 " } [2]=> array(3) { [0]=> string(0) "" [1]=> int(2) [2]=> string(3) "30 " } [3]=> array(3) { [0]=> string(0) "" [1]=> int(3) [2]=> string(3) "35 " } [4]=> array(3) { [0]=> string(0) "" [1]=> int(4) [2]=> string(2) "40" } }

please help me thanks alot 请帮我多谢

Reza, per your vardump, the values in your array are strings and the array you showed in your question had ints. Reza,按照您的标准,数组中的值是字符串,并且您在问题中显示的数组是整数。 Here is one way to convert these strings to ints. 这是将这些字符串转换为int的一种方法。

    for ($j=0; $j<=10; $j++){
        $tmp=explode("   ", $members[$j]);
        for($k=0; $k<count($tmp); $k++){
            $tmp[$k] = intval($tmp[$k]);
        }
        $parts[$j]=$tmp;     
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM