简体   繁体   English

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

[英]How to assign an array inside a while loop in php

I'm trying to read values from a .txt file, line by line, then store it inside of an array so that I can use those values later in my program.我正在尝试从 .txt 文件中逐行读取值,然后将其存储在数组中,以便稍后在我的程序中使用这些值。 The problem is that when I print my array inside of the loop, it prints just fine but when I try to print it outside of loop it doesn't print anything.问题是,当我在循环内打印数组时,它打印得很好,但是当我尝试在循环外打印它时,它不打印任何内容。

txt file: txt文件:

I/P voltage : 212.0输入电压:212.0
I/P fault voltage : 212.0 I/P 故障电压:212.0
O/P voltage : 212.0输出电压:212.0
O/P current : 000输出电流:000
I/P frequency : 50.0输入/输出频率:50.0
Battery voltage : 13.7电池电压:13.7
Temperature : 28.0温度:28.0
UPS Status : 00001001 UPS 状态:00001001

my code:我的代码:

array name is $UPS_respond数组名称是$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>";
?>

result:结果:

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 => 

@Digital_affection @Digital_affect

Can you please try following way?你能试试下面的方法吗? Hope it may help you.希望它可以帮助你。

<?php
# -----------------------------------------------------
# Read value from file
# -----------------------------------------------------

    $i      = 0 ;
    $file   = fopen("/usr/local/bin/UpsResult.txt","r"); //i open my file to read it
    $dot    = 0;
    $result_arr = [];
    while( !feof( $file ) ) {
        $y      = fgets($file);
        $dot    = strpos($y,':')+1; 
        $x      = substr($y, $dot);
        $result_arr[] = $x;
        // $UPS_respond = array($i => $x);
        // echo "inside of Loop => ".'$UPS_respond['.$i.'] :'.$UPS_respond[$i]."<br>"; 
        $i++;
    }
    fclose( $file );
    //echo "<pre>Ouside of Loop => ".$UPS_respond[$i]."<br>";
    echo "<pre>Ouside of Loop => "; print_r( $result_arr );

?>

Result would be like:结果会是这样的:

Ouside of Loop => Array
(
    [0] =>  212.0

    [1] =>  212.0

    [2] =>  212.0

    [3] =>  000

    [4] =>  50.0

    [5] =>  13.7

    [6] =>  28.0

    [7] =>  00001001
)

You have to add $UPS_respond = [] before the while loop.您必须在while循环之前添加$UPS_respond = [] You also have to change你也必须改变

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

to

$UPS_respond[$i] = $x;

The reason for this is, on each iteration you are replacing the array with a new one.这样做的原因是,在每次迭代中,您都用新的数组替换数组。 And with the above code, you will add the value to the array, instead of creating a new one each time.使用上面的代码,您会将值添加到数组中,而不是每次都创建一个新值。

UPDATE : I saw another issue.更新:我看到了另一个问题。 It is with the echo at the end.最后是echo You have all the values in the array , but you are only printing the last one, because $i is the key to the last array.您拥有array中的所有值,但您只打印最后一个,因为$i是最后一个数组的键。

You can check with by doing你可以通过做检查

var_dump($UPS_respond);

If you tell me, how exactly you want to use the values, I can tell you how to handle it.如果你告诉我,你想如何使用这些值,我可以告诉你如何处理它。

Original answer:原答案:

You can make things easier with the following approach.您可以使用以下方法使事情变得更容易。 Use file() and explode() PHP functions to read the content of your file and to parse the content of each line.使用file()explode() PHP 函数来读取文件的内容并解析每一行的内容。

<?php
// Read file
$UPS_respond = file('UpsResult.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// Output
foreach ($UPS_respond as $line) {
    $a = explode(':', $line);
    echo "Item info: ".$a[0]." : ".$a[1]."<br>"; 
};
?>

Output:输出:

Item info: I/P voltage : 212.0
Item info: I/P fault voltage : 212.0
Item info: O/P voltage : 212.0
Item info: O/P current : 000
Item info: I/P frequency : 50.0
Item info: Battery voltage : 13.7
Item info: Temperature : 28.0
Item info: UPS Status : 00001001

Update:更新:

If you want to get only part of your lines, use the next approach.如果您只想获取部分行,请使用下一种方法。 The reason for the error in your script is that you need to add an item in the $UPS_respond array with $UPS_respond[] = ...;你的脚本出错的原因是你需要在$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) {
    $a = explode(':', $line);
    $UPS_respond[] = $a[1];
};
var_dump($UPS_respond);
// ?>

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

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