2

I am a student, currently learning PHP and having a tough time getting the explode() function to work the way I want.

The following text file "vacations.txt" contains:

"54321", "Big Island Hawaii", "2999.00", "Best beaches big volcano"
"87654", "Cruise Caribbean", "3500.00", "Ocean view with balcony Cancun Jamaica etc."
"09876", "Rome and Madrid", "3999.00", "I see Italy I see Spain"
"32198", "Ski Tahoe", "1200.00", "Ski all day Gamble all night"

I have tried to put this into an array delimited by the comma. Using print_r(), I can see that it's all going into the first index of my array. Here's my code:

function displaySpecials() {
    $prodString = include("vacation.txt");
    $vacArray = explode(',', $prodString ); 
        print_r($vacArray);
}

Here is the output:

"54321", "Big Island Hawaii", "2999.00", "Best beaches big volcano" "87654", "Cruise Caribbean", "3500.00", "Ocean view with balcony Cancun Jamaica etc." "09876", "Rome and Madrid", "3999.00", "I see Italy I see Spain" "32198", "Ski Tahoe", "1200.00", "Ski all day Gamble all night"Array ( [0] => 1 )

I have searched, and read everything I can find about explode(), but I cannot figure out why this is happening. My end goal is to output a multidimentional array in a table with 4 rows and 4 columns to display the 16 values in the text file.

All help is greatly appreciated. Thank you in advance!

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
01001101
  • 33
  • 5
  • Not exactly sure what your expected output is, but maybe take a look here: http://stackoverflow.com/a/12286933/3933332 – Rizier123 Apr 10 '16 at 05:14

3 Answers3

2

The include statement includes and evaluates the specified file.

include will evaluate whatever is in it, basically to include other php script.

You need to fetch its content.

Below I have used file to fetch contents of file linewise.

function displaySpecials() {
    $lines = file("vacation.txt"); // will return lines in array
    foreach($lines as $line){ // loop for each line
        $vacArray[] = explode(",", $line); // insert resulting array to last index of $varArray
    } 
    print_r($vacArray);
    return $vacArray;
}

Also, the file appears to be regular .csv you can use fgetcsv. Check the example in the official docs.

Jigar
  • 3,256
  • 1
  • 30
  • 51
0

Try to replace include("vacation.txt") with file_get_contents("vacation.txt").

include

The include statement includes and evaluates the specified file.

file_get contents

Reads entire file into a string

Your code should look like this:

function displaySpecials() {
    $prodString = file_get_contents("vacation.txt");
    $vacArray = explode(',', $prodString ); 
        print_r($vacArray);
}
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
0

Since this is basically a csv file, I'd recommend using fgetcsv, something like this:

if (($h = fopen('vacation.txt', 'r')) !== false) {
    while (($data = fgetcsv($h, 1000, ',')) !== false) {
        // Fetch the data here
    }
}

Optionally, you can also use str_getcsv:

$data = array_map('str_getcsv', file('vacation.txt'));
Eihwaz
  • 1,234
  • 10
  • 14