Berlian Berlian - 26 days ago 15
PHP Question

Import text file into one dimentional array in PHP

I import a text file, here the code:

<?php
$file = fopen("stoplist.txt","r") or die("fail to open file");
$stoplist;
$i = 0;
while($row = fgets($file)){
$data = explode(",", $row);
$stoplist[$i] = $data;
$i++;
}
fclose($file);

print "<pre>";
print_r($stoplist);
print "</pre>"


?>


and it has an output like this:

Array
(
[0] => Array
(
[0] => a

)

[1] => Array
(
[0] => able

)

[2] => Array
(
[0] => about

)

[3] => Array
(
[0] => above

)
)


But I actually want a result like this:

Array
(
[0] => a
[1] => able
[2] => about
[3] => above
)


Do you have any suggestion for my problem?

Answer

You can use array_merge to convert your array into single Dimensional array:

$onedimension= call_user_func_array('array_merge', $stoplist);
print "<pre>";
print_r($onedimension);
Comments