【问题标题】:How to assign an array inside a while loop in php如何在php中的while循环内分配一个数组
【发布时间】:2019-12-12 16:47:06
【问题描述】:

我正在尝试从 .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 => 

【问题讨论】:

  • echo 'Well bugger me, turns out after the loop $i has the following value: ' . $i;
  • 如果你输入echo $i,你会发现它的值是8,因为它在退出循环之前最后一次递增,并且数组没有分配给该键的值。

标签: php arrays variable-assignment assign


【解决方案1】:

您必须在 while 循环之前添加 $UPS_respond = []。 你也得改变

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

$UPS_respond[$i] = $x;

这样做的原因是,在每次迭代中,您都在用一个新数组替换数组。使用上面的代码,你将把值添加到数组中,而不是每次都创建一个新的。

更新:我看到了另一个问题。最后是echo。您拥有array 中的所有值,但您只打印最后一个,因为$i 是最后一个数组的键。

你可以通过做检查

var_dump($UPS_respond);

如果你告诉我,你究竟想如何使用这些值,我可以告诉你如何处理它。

【讨论】:

  • 不行,还是不行。感谢您回答顺便说一句
  • 它给出了类似于循环内部 => $UPS_respond[0] :循环内部的数组 => $UPS_respond[1] :循环内部的数组 => $UPS_respond[2] :内部的数组循环 => $UPS_respond[3] : 循环内数组 => $UPS_respond[4] : 循环内数组 => $UPS_respond[5] : 循环内数组 => $UPS_respond[6] : 循环内数组=> $UPS_respond[7] : 循环外的数组 =>
  • 感谢您的帮助马丁。因为我怀疑问题是分配数组,Ram Chander 解决了它。
  • 问题不是你解释的。问题只是我分配数组的方式。
  • 你说得对。对不起。我是一个 php 菜鸟,所以我不完全理解你!我的坏
【解决方案2】:

@Digital_affection

你能试试下面的方法吗?希望对您有所帮助。

<?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 );

?>

结果如下:

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
)

【讨论】:

  • 非常感谢拉姆!我的问题解决了!不知道是什么!
  • 很高兴听到这个消息:)
【解决方案3】:

原答案:

您可以使用以下方法使事情变得更容易。使用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>"; 
};
?>

输出:

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

更新:

如果您只想获取部分行,请使用下一种方法。你的脚本出错的原因是你需要在$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);
// ?>

【讨论】:

  • 看起来不错,但我只想读取每一行中的数字。例如在第 1 行我只需要 212.0
  • @Digital_affection 你的错误原因是你需要使用$UPS_respond[] = ...;在数组中添加一个项目。
猜你喜欢
  • 1970-01-01
  • 2014-05-29
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
相关资源
最近更新 更多