PHP reads file to array

I have a file (Test.txt) with the following data: 1.0

I want to read the contents of this file in an array, and then print the variables. Here is my current code:

    function readUserDetails($username) {
    $userDetails = explode(',', file($username.".txt"));
    print($userDetails[0].$userDetails[1]);
}

If I call the readUserDetails function with the following parameters: "Test", I get the following errors:

Note. Converting an array to a string in C: \ Users \ s13 \ Game \ Game6 \ default.php on line 128 Note: Undefined offset: 1 in C: \ Users \ s13 \ Game \ Game6 \ default.php on line 129 Array

Can I get some help to make this work?

+3
source share
4 answers

file($username.".txt")already returning you an array and you are trying to detonate the array with ,delimeter

try it

function readUserDetails($username) {
  $userDetails = explode(',', file_get_contents($username.".txt"));
  print($userDetails[0].$userDetails[1]);
}
+1
source

PHP file() , . . http://php.net/manual/en/function.file.php. explode() . , file(), , :

<?php
   ...
   $userDetails = file($username.".txt");
   print_r($userDetails);
?>
+3

You can use file_get_contents () to return the contents of the file as a string.

$ userDetails = explode (',', file_get_contents ($ username. ". txt"));

0
source

Try:

$ array = explode ("\ n", file_get_contents ($ username.'txt '));

0
source

All Articles