如何在php中的while循环内分配数组

我正在尝试从 .txt 文件中逐行读取值,然后将其存储在数组中,以便稍后在我的程序中使用这些值。问题是,当我在循环内打印数组时,它打印得很好,但是当我尝试在循环外打印它时,它不打印任何内容。


txt文件:

I/P 电压:212.0

I/P 故障电压:212.0

O/P 电压:212.0

O/P 电流:000

I/P 频率:50.0

电池电压:13.7

温度:28.0

UPS 状态:00001001


我的代码:

数组名称是 $UPS_respond


<?php

# -----------------------------------------------------

# Read value from file

# -----------------------------------------------------


    $i      = 0 ;

    $file   = fopen("/usr/local/bin/UpsResult.txt","r"); //i open my file to read it

    $dot    = 0;


    while( !feof( $file ) ) {

        $y      = fgets($file);

        $dot    = strpos($y,':')+1; 

        $x      = substr($y, $dot);

        $UPS_respond = array($i => $x);

        echo "inside of Loop => ".'$UPS_respond['.$i.'] :'.$UPS_respond[$i]."<br>"; 

        $i++;

    }

    fclose( $file );

    echo "Ouside of Loop => ".$UPS_respond[$i]."<br>";

?>

结果:

inside of Loop => $UPS_respond[0] : 213.5 

inside of Loop => $UPS_respond[1] : 213.5 

inside of Loop => $UPS_respond[2] : 213.0 

inside of Loop => $UPS_respond[3] : 000 

inside of Loop => $UPS_respond[4] : 50.0 

inside of Loop => $UPS_respond[5] : 13.7 

inside of Loop => $UPS_respond[6] : 28.0 

inside of Loop => $UPS_respond[7] : 00001001

Ouside of Loop => 


呼如林
浏览 148回答 3
3回答

至尊宝的传说

你能试试下面的方法吗?希望它可以帮助你。<?php# -----------------------------------------------------# Read value from file# -----------------------------------------------------&nbsp; &nbsp; $i&nbsp; &nbsp; &nbsp; = 0 ;&nbsp; &nbsp; $file&nbsp; &nbsp;= fopen("/usr/local/bin/UpsResult.txt","r"); //i open my file to read it&nbsp; &nbsp; $dot&nbsp; &nbsp; = 0;&nbsp; &nbsp; $result_arr = [];&nbsp; &nbsp; while( !feof( $file ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $y&nbsp; &nbsp; &nbsp; = fgets($file);&nbsp; &nbsp; &nbsp; &nbsp; $dot&nbsp; &nbsp; = strpos($y,':')+1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $x&nbsp; &nbsp; &nbsp; = substr($y, $dot);&nbsp; &nbsp; &nbsp; &nbsp; $result_arr[] = $x;&nbsp; &nbsp; &nbsp; &nbsp; // $UPS_respond = array($i => $x);&nbsp; &nbsp; &nbsp; &nbsp; // echo "inside of Loop => ".'$UPS_respond['.$i.'] :'.$UPS_respond[$i]."<br>";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; }&nbsp; &nbsp; fclose( $file );&nbsp; &nbsp; //echo "<pre>Ouside of Loop => ".$UPS_respond[$i]."<br>";&nbsp; &nbsp; echo "<pre>Ouside of Loop => "; print_r( $result_arr );?>结果会是这样的:Ouside of Loop => Array(&nbsp; &nbsp; [0] =>&nbsp; 212.0&nbsp; &nbsp; [1] =>&nbsp; 212.0&nbsp; &nbsp; [2] =>&nbsp; 212.0&nbsp; &nbsp; [3] =>&nbsp; 000&nbsp; &nbsp; [4] =>&nbsp; 50.0&nbsp; &nbsp; [5] =>&nbsp; 13.7&nbsp; &nbsp; [6] =>&nbsp; 28.0&nbsp; &nbsp; [7] =>&nbsp; 00001001)

慕妹3242003

原答案:您可以使用以下方法使事情变得更容易。使用file()和explode()PHP 函数读取文件内容并解析每一行的内容。<?php// Read file$UPS_respond = file('UpsResult.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);// Outputforeach ($UPS_respond as $line) {&nbsp; &nbsp; $a = explode(':', $line);&nbsp; &nbsp; echo "Item info: ".$a[0]." : ".$a[1]."<br>";&nbsp;};?>输出:Item info: I/P voltage : 212.0Item info: I/P fault voltage : 212.0Item info: O/P voltage : 212.0Item info: O/P current : 000Item info: I/P frequency : 50.0Item info: Battery voltage : 13.7Item info: Temperature : 28.0Item info: UPS Status : 00001001更新:如果您只想获取部分行,请使用下一种方法。脚本中出现错误的原因是您需要在$UPS_respond数组中添加一个$UPS_respond[] = ...;.<?php// Read file$file = file('UpsResult.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);// Output$UPS_respond = array();foreach ($file as $line) {&nbsp; &nbsp; $a = explode(':', $line);&nbsp; &nbsp; $UPS_respond[] = $a[1];};var_dump($UPS_respond);// ?>

慕哥9229398

您必须$UPS_respond = []在while循环之前添加。你也必须改变$UPS_respond&nbsp;=&nbsp;array($i&nbsp;=>&nbsp;$x);到$UPS_respond[$i]&nbsp;=&nbsp;$x;这样做的原因是,在每次迭代中,您都用新的数组替换数组。使用上面的代码,您会将值添加到数组中,而不是每次都创建一个新值。更新:我看到了另一个问题。它echo在最后。您拥有 中的所有值array,但您只打印最后一个,因为$i是最后一个数组的键。你可以通过做检查var_dump($UPS_respond);如果你告诉我,你到底想如何使用这些值,我可以告诉你如何处理它。
打开App,查看更多内容
随时随地看视频慕课网APP