简体   繁体   中英

how to convert text file to array in php?

1) i have a text file (converted.txt) and that content's users info:

 0   hbhenet      pppoe   D4:CA:6D:F1:5D:5A 10.23.0.254     8h1m36s            
 1 R moh@hj       pppoe   00:27:22:16:3B:F2 10.17.0.253     8h1m36s            
 2 R omarmool@dr  pppoe   68:72:51:3A:53:1B 10.17.0.251     8h1m36s            
 3 R admin@khr2   pppoe   24:A4:3C:9F:83:10 10.17.0.252     8h1m36s            
 4 R yas@ahmed.m  pppoe   24:A4:3C:E6:DD:3E 10.17.0.250     8h1m36s            
 5 R hus@ahmed.m  pppoe   44:D9:E7:DA:32:95 10.16.0.246     8h1m36s        

2) and what i have done in my php is :

$file="converted.txt";
$fopen = fopen($file, "r");
$fread = fread($fopen,filesize($file));
fclose($fopen);

$remove = "\n";
$split = explode($remove, $fread);
$array[] = NULL;
$tab = "\t";

foreach ($split as $string)
{
    $row = explode($tab, $string);
    array_push($array,$row);
}

echo "<pre>";
print_r($array);
echo "</pre>";

3) the output is like :

Array
(

    [0] => 
[1] => Array
    (
        [0] =>  0   hbhenet      pppoe   D4:CA:6D:F1:5D:5A 10.23.0.254     8h1m36s            
    )

[2] => Array
    (
        [0] =>  1 R moh@hj       pppoe   00:27:22:16:3B:F2 10.17.0.253     8h1m36s            
    )

[3] => Array
    (
        [0] =>  2 R omarmool@dr  pppoe   68:72:51:3A:53:1B 10.17.0.251     8h1m36s            
    )

[4] => Array
    (
        [0] =>  3 R admin@khr2   pppoe   24:A4:3C:9F:83:10 10.17.0.252     8h1m36s            
    )

[5] => Array
    (
        [0] =>  4 R yas@ahmed.m  pppoe   24:A4:3C:E6:DD:3E 10.17.0.250     8h1m36s            
    )

[6] => Array
    (
        [0] =>  5 R hus@ahmed.m  pppoe   44:D9:E7:DA:32:95 10.16.0.246     8h1m36s        
    )

 )

4) but what i want is :

Array(

[0] => Array
    (
        [0] => Array
            (
                [0] => 0
                [1] => 
                [2] => hbhenet
                [3] => pppoe
                [4] => D4:CA:6D:F1:5D:5A
                [5] => 10.23.0.254
                [6] => 8h1m36s
            )
        [1] => Array
            (
                [0] => 1
                [1] => R
                [2] => moh@hj
                [3] => pppoe
                [4] => 00:27:22:16:3B:F2
                [5] => 10.17.0.253
                [6] => 8h1m36s
            )
        [2] => Array
            (
                [0] => 2
                [1] => R
                [2] => omarmool@dr
                [3] => pppoe
                [4] => 68:72:51:3A:53:1B
                [5] => 10.17.0.251
                [6] => 8h1m36s
            )
        [3] => Array
            (
                [0] => 3
                [1] => R
                [2] => admin@khr2
                [3] => pppoe
                [4] => 24:A4:3C:9F:83:10
                [5] => 10.17.0.252
                [6] => 8h1m36s
            )
        [4] => Array
            (
                [0] => 4
                [1] => R
                [2] => yas@ahmed.m
                [3] => pppoe
                [4] => 24:A4:3C:E6:DD:3E
                [5] => 10.17.0.250
                [6] => 8h1m36s
            )
        [5] => Array
            (
                [0] => 5
                [1] => R
                [2] => hus@ahmed.m
                [3] => pppoe
                [4] => 44:D9:E7:DA:32:95
                [5] => 10.16.0.246
                [6] => 8h1m36s
            )
    )
 )

any idea? thanks in advance.

I have created a file with name myfile.txt :-

 0   hbhenet      pppoe   D4:CA:6D:F1:5D:5A 10.23.0.254     8h1m36s            
 1 R moh@hj       pppoe   00:27:22:16:3B:F2 10.17.0.253     8h1m36s            
 2 R omarmool@dr  pppoe   68:72:51:3A:53:1B 10.17.0.251     8h1m36s            
 3 R admin@khr2   pppoe   24:A4:3C:9F:83:10 10.17.0.252     8h1m36s            
 4 R yas@ahmed.m  pppoe   24:A4:3C:E6:DD:3E 10.17.0.250     8h1m36s            
 5 R hus@ahmed.m  pppoe   44:D9:E7:DA:32:95 10.16.0.246     8h1m36s        

Now i have applied below code:-

<?php
$array = explode("\n", file_get_contents('myfile.txt'));

echo "<pre/>";print_r($array);

And output is :-

Array
(
    [0] =>  0   hbhenet      pppoe   D4:CA:6D:F1:5D:5A 10.23.0.254     8h1m36s            
    [1] =>  1 R moh@hj       pppoe   00:27:22:16:3B:F2 10.17.0.253     8h1m36s            
    [2] =>  2 R omarmool@dr  pppoe   68:72:51:3A:53:1B 10.17.0.251     8h1m36s            
    [3] =>  3 R admin@khr2   pppoe   24:A4:3C:9F:83:10 10.17.0.252     8h1m36s            
    [4] =>  4 R yas@ahmed.m  pppoe   24:A4:3C:E6:DD:3E 10.17.0.250     8h1m36s            
    [5] =>  5 R hus@ahmed.m  pppoe   44:D9:E7:DA:32:95 10.16.0.246     8h1m36s        
)

Now a workaround so that no need to change anything in your text file is given below (complete code):-

<?php
$array = explode("\n", file_get_contents('myfile.txt'));

echo "<pre/>";print_r($array);
$new_array = array();
foreach($array as $key =>&$arr){

    if ($arr[3] !=='R'){

        $arr[3] ="_";
    }
    $parts = preg_split('/\s+/', trim($arr));
    if($parts[1] == '_'){
        $parts[1] = '';
    }
    $new_array[$key] = $parts;
}

echo "<pre/>";print_r($new_array);

Note:- it will give you exact desired output what you shown to us (I have tested it at my local end)

What you asked in comment for that do like below:-

<?php
$array = explode("\n", file_get_contents('myfile.txt'));

echo "<pre/>";print_r($array);
$new_array = array();
$indexed_array = Array('number','flag','name','service','mac','IP','uptime');

foreach($array as $key =>&$arr){

    if ($arr[3] !=='R'){

        $arr[3] ="_";
    }
    $parts = preg_split('/\s+/', trim($arr));
    if($parts[1] == '_'){
        $parts[1] = '';
    }
    $new_array[$key] = array_combine($indexed_array,$parts);
}

echo "<pre/>";print_r($new_array);
    <?php
    $lines = array();
    $fopen = fopen('converted.txt', 'r');
    while (!feof($fopen)) {
        $line=fgets($fopen);
        $line=trim($line);
        $lines[]=$line;

    }
    fclose($fopen);
    $finalOutput = array();
    foreach ($lines as $string)
    {
        $string = preg_replace('!\s+!', ' ', $string);
        $row = explode(" ", $string);
        array_push($finalOutput,$row);
    }
    echo "<pre>";
    print_r($finalOutput);
    echo "</pre>";
?>

and Output is

    Array
(
    [0] => Array
        (
            [0] => 0
            [1] => hbhenet
            [2] => pppoe
            [3] => D4:CA:6D:F1:5D:5A
            [4] => 10.23.0.254
            [5] => 8h1m36s
        )

    [1] => Array
        (
            [0] => 1
            [1] => R
            [2] => moh@hj
            [3] => pppoe
            [4] => 00:27:22:16:3B:F2
            [5] => 10.17.0.253
            [6] => 8h1m36s
        )

    [2] => Array
        (
            [0] => 2
            [1] => R
            [2] => omarmool@dr
            [3] => pppoe
            [4] => 68:72:51:3A:53:1B
            [5] => 10.17.0.251
            [6] => 8h1m36s
        )

    [3] => Array
        (
            [0] => 3
            [1] => R
            [2] => admin@khr2
            [3] => pppoe
            [4] => 24:A4:3C:9F:83:10
            [5] => 10.17.0.252
            [6] => 8h1m36s
        )

    [4] => Array
        (
            [0] => 4
            [1] => R
            [2] => yas@ahmed.m
            [3] => pppoe
            [4] => 24:A4:3C:E6:DD:3E
            [5] => 10.17.0.250
            [6] => 8h1m36s
        )

    [5] => Array
        (
            [0] => 5
            [1] => R
            [2] => hus@ahmed.m
            [3] => pppoe
            [4] => 44:D9:E7:DA:32:95
            [5] => 10.16.0.246
            [6] => 8h1m36s
        )

)

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