0

Error using explod : Parse error: syntax error, unexpected '$lines' (T_VARIABLE)

I want to extract content of a text file (3 lines) in an array, then explode this array by "," delimiter, then explode each value by ":" delimiter.

For the first line it works, not for the second... Don't know why.

Content of the file:

1:a,2:b,3:c
10:a,5:b,4:c
8:c,9:a,3:b
$lines = file('test.txt');

$top_table = explode(",", $lines[0]);
foreach ($top_table as $key => $str)
{
$top_table[$key] = explode(":", $str);
}

$top_splits = explode("," $lines[1]);
foreach ($top_splits as $key => $str)
{
$top_splits[$key] = explode(":", $str);
}

This line : $top_splits = explode("," $lines[1]); is throwing the error. If I print $lines[1], it works so I don't know why first explode on $lines[0] then first foreach work well, but not the second one.

tonio94
  • 460
  • 1
  • 6
  • 18
  • 2
    The cause of this error is that you forgot the comma separator for the parameters in the call of explode function (line 10) ;) – Marcelino Feb 07 '19 at 15:47
  • Omg! I have been searching and comparing my code for 30min without seeing this.... Thank you! – tonio94 Feb 07 '19 at 15:53

0 Answers0