简体   繁体   中英

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. 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:

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

my code:

array name is $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

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

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.

<?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[] = ...; .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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