Alejandro Alejandro - 2 months ago 6
PHP Question

Fill table with a txt file in PHP

I have a txt file with the following structure

12355 987564

35122 894843


88488 645454

45556 444442

My code:

<?php
$array = array_map('trim', explode("\n", file_get_contents('stats/S6090615EMV.D060')));

foreach ($array as $data) {
echo $data;
}

?>


now I do not know how to fill a table with the values of each line, each value in a column.

I can read each line of the file, but not divide the elements within each line.

Please help me.
Using explode , I get as a result
12355 987564

I want this:

12355

987564

SOLVED

<?php
foreach ($array as $data) {
$row = explode(" ", $data);
?>
<tr>
<td> <?php var_dump($data); ?> </td>
<td><?php echo $row[0]; ?></td>
<td><?php echo $row[1]; ?></td>
</tr>
<?php } ?>


thanks !

Answer

You will first want to use file(), it's made to take a file and split the rows into an array by default. Use explode() on the the rest:

$new     =  array();
$fileArr = array_map(function($v) {
    return explode(' ',trim($v)); 
},file('stats/S6090615EMV.D060',FILE_SKIP_EMPTY_LINES));

foreach($fileArr as $rows) {
    $new    =   array_merge($new,$rows);
}

$new = array_filter($new);
print_r($new);

Gives you:

Array
(
    [0] => 12355
    [1] => 987564
    [2] => 35122
    [3] => 894843
    [5] => 88488
    [6] => 645454
    [7] => 45556
    [8] => 444442
)