9

I have stored array in txt file using file_put_contents() in php, php array write successfullu in text file as same time how to read that text file into php?

<?php
$arr = array('name','rollno','address');
file_put_contents('array.txt', print_r($arr, true));
?>

The above php write text file in successfully. i want read that text file in php?

5
  • don't use print_r use var_export instead Commented Jul 27, 2016 at 4:23
  • what is the diff b/w those two var_export and print_r ? Commented Jul 27, 2016 at 5:01
  • if you plan on reusing those values from the array, you'd better of using var_export or just create a json file Commented Jul 27, 2016 at 5:02
  • i have used var_export and then use $strarray = file_get_contents('input_array_export.txt'); and my input_array_export.txt contain array ( 'select' => array ( 189 => array ( 'vl' => array ( 0 => '2', 1 => '1', ) and then i have tried to print_r($strarray['select']); it has return a Commented Jul 27, 2016 at 5:04
  • how i can access select index from readed array Commented Jul 27, 2016 at 5:05

4 Answers 4

20

If you plan on reusing those same values inside the array, you could use var_export on creating that array file instead.

Basic example:

$arr = array('name','rollno','address');
file_put_contents('array.txt',  '<?php return ' . var_export($arr, true) . ';');

Then, when the time comes to use those values, just use include:

$my_arr = include 'array.txt';
echo $my_arr[0]; // name

Or just use a simple JSON string, then encode / decode:

$arr = array('name','rollno','address');
file_put_contents('array.txt',  json_encode($arr));

Then, when you need it:

$my_arr = json_decode(file_get_contents('array.txt'), true);
echo $my_arr[1]; // rollno
Sign up to request clarification or add additional context in comments.

1 Comment

Thank u, i got Expected result, but i have doubt from '<?php return ' . var_export($InputArr, true) . ';' why we use return i don't know can you explain
2

Use this one:

$arr = file('array.txt', FILE_IGNORE_NEW_LINES);
print_r($arr);

Comments

0

you can simply try to read the file as below

$strarray = file_get_contents('array.txt');

Then you can use the array as per your need.

Comments

0

fopen()

The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened

fread()

The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

fclose()

The fclose() function is used to close an open file.

$arr = array('name','rollno','address');
file_put_contents('array.txt', print_r($arr, true));

$myfile = fopen("array.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("array.txt"));
fclose($myfile);

Output:

Array ( [0] => name [1] => rollno [2] => address )

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.