0

i got a text file that contains

a b c 

a c

b

i want to read the text file and convert it to array of arrays like this

  array(
        array('a', 'b', 'c'),
        array('a', 'c'),
        array('b'));

i want to split each line in an array and each word in a index in the array

i tried this code but it only split each line in a array i want each word in a array of the line

$file="140724.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>";

i got this result

Array

(

    [0] => 

    [1] => Array
        (
            [0] => a b c
        )

    [2] => Array
        (
            [0] => a b
        )

    [3] => Array
        (
            [0] => c
        ))

HP371
  • 860
  • 11
  • 24

5 Answers5

3

You can reduce the code quite a bit by using some of the inbuilt functions.

Using file() with the appropriate flags will read the file in, ignoring any empty lines and return an array of data.

Then use array_map() along with explode() to process this array and split it into the individual parts per row...

$fileName = "140724.txt";
$file = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$output = array_map(function($line) { return explode("\t", $line); }, $file);

will produce

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [1] => Array
        (
            [0] => a
            [1] => c
        )

    [2] => Array
        (
            [0] => b
        )

)
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

Try with like this.

$file="140724.txt";
$fopen = fopen($file, r);
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = "\n";
$split = explode($remove, $fread);
$array = array();
$tab = " ";
foreach ($split as $string)
{
    $array[] = explode($tab, $string);
}
echo "<pre>";
print_r($array);
echo "</pre>";

Hope this help to you.

HP371
  • 860
  • 11
  • 24
0

There are couple of errors.

First - init the $array var as $array = []; and not $array[] = null;.

Second, you can use fgetcsv which will be better.

Last, I think the delimiter is (white space) and not \t.

Consider:

$handle = fopen($file, "r");
while (($data = fgetcsv($handle, 1000, " ")) !== FALSE) {
    $arr[] = $data;
}
dWinder
  • 11,597
  • 3
  • 24
  • 39
0

This will give perfect output without blank array.Thank me later.

$file="140724.txt";
$fopen = fopen($file, 'r');
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = "\n";
$split = explode($remove, $fread);

$custom_array = array();
$x = 0;
foreach ($split as $value) {
    $value = trim($value);
    if($value != '' || $value != null){
        $inside_explode = explode(" ", $value);
        $custom_array[$x] = $inside_explode;
        $x++;
    }


}
print_r($custom_array);
Ritesh Dhoke
  • 169
  • 10
0
$fileData = file('1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$result = [];
foreach ($fileData as $row) {
    $splitRow = str_getcsv($row, ' ');
    $result[] = $splitRow;
}

echo '<pre>';
print_r($result);
$fileData = file('1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$result = array_map(function ($row) {
    return str_getcsv($row, ' ');
}, $fileData);

echo '<pre>';
print_r($result);
Aleksandr
  • 401
  • 3
  • 7