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
        ))

5 Answers 5

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
        )

)
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

This code may work but it doesn't explain the issues in the OP code...
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;
}

Comments

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);

1 Comment

Please add some explanation to your code such that others can learn from it
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);

1 Comment

This code may work but it doesn't explain the issues in the OP code...

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.