0

I am getting String from txt file, and then explode to get format below.

$array = file('test.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach($array as $arr) {
    print_r(explode(";",$arr));
}

Output is:

Array ( [0] => miha [1] => dasjkhdkasjhdjkashdjka [2] => Paracinac [3] => Kupus, Krastavac, Majonez, Vegeta, Aleva, Tartar [4] => dasdas ) 
Array ( [0] => miha [1] => dasjkhdkasjhdjkashdjka [2] => Paracinac [3] => Kupus, Krastavac, Majonez, Vegeta, Aleva, Tartar [4] => dasdas )

INFO: txt file will have lots of data since its add dynamically by users inputs so ill have like 30 arrays every day.

Now i am trying to make a table, one row for each array and table data for every element of array. I tryed code:

foreach ($array as $row) {
    echo '<tr>';
        echo '<td>' . $row['0'] . '</td>';
        echo '<td>' . $row['1'] . '</td>';
        echo '<td>' . $row['2'] . '</td>';
        echo '<td>' . $row['3'] . '</td>';
        echo '<td>' . $row['4'] . '</td>';
    echo '</tr>';
}

And i am getting out put like

m   i   h   a   ;
m   i   h   a   ;

So i noticed that $row['0']... 0 is point of first character of first element in array but i need full value.

1
  • you forgot explode $row in your second loop ;) Commented Jun 17, 2016 at 12:12

1 Answer 1

1

You need to store exploded array in any array then you need to access that array, Try:

$loopArr = array();
$array = file('test.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($array as $arr) {
    $loopArr[] = explode(";",$arr); // first create array of all exploded data
}

if(!empty($loopArr)) { // loop through that array
    foreach($loopArr as $arrInner) { // display data inside
        echo '<tr>';
            echo '<td>' . $arrInner['0'] . '</td>';
            echo '<td>' . $arrInner['1'] . '</td>';
            echo '<td>' . $arrInner['2'] . '</td>';
            echo '<td>' . $arrInner['3'] . '</td>';
            echo '<td>' . $arrInner['4'] . '</td>';
        echo '</tr>';
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wors very fine. Thxs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.