0
PPP TXT:
123 45678 8888 
123 45678 8888
123 45678 8888

$file = file('PPP.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($file as $line) {
$words = explode(" ", $line);
print_r($words);
}

output array:

Array ( 
    [0] => 123 
    [1] => 45678 
    [2] => 8888 
) 
Array ( 
    [0] => 123 
    [1] => 45678 
    [2] => 8888 
) 
Array ( 
    [0] => 123 
    [1] => 45678 
    [2] => 8888 
)

BUT I want output array

Array [0] ( 
    [0] => 123 
    [1] => 45678 
    [2] => 8888 
) 
Array[1] ( 
    [0] => 123 
    [1] => 45678 
    [2] => 8888 
) 
Array[3] ( 
    [0] => 123 
    [1] => 45678 
    [2] => 8888 
)

Thank you.

1
  • 1
    $words[] = explode(" ", $line); Commented Jul 1, 2020 at 6:56

1 Answer 1

2

If you want to store them as a multidimensional array, push each row into an array instead of separate variables.

$file = file('PPP.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// Instantiate the array we will store each result in
$words = [];

foreach ($file as $line) {
    // Push the result into the array
    $words[] = explode(" ", $line);
}

// Dump the array after the loop to get all of them
print_r($words);
Sign up to request clarification or add additional context in comments.

Comments

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.