0

I have an array something like below:

Array
(
    [0] => IA62b066
    [1] => IAca99de
    [2] => IAafed13
    [3] => IA49882c
    [4] => IA5c9872
    [5] => IA39b3e1

I want to export the only the value from this array to text file. Txt file should display data like this.

IA62b066
IAca99de
IAafed13
IA49882c
IA5c9872
IA39b3e1

I have tried many ways but I could not solve it.

file_put_contents("output/$fileName.txt", print_r($array, true));

Can anybody please help me solve it.

nas
  • 2,289
  • 5
  • 32
  • 67

2 Answers2

3

That will create test.txt file in the directory of php script with the content of the array.

<?php

$input = [
    'IA62b066',
    'IAca99de',
    'IAafed13',
    'IA49882c',
    'IA5c9872',
    'IA39b3e1'
];

$filePath = __DIR__ . DIRECTORY_SEPARATOR . 'test.txt';
$content = implode(PHP_EOL, $input);
file_put_contents($filePath, $content);
Jimmix
  • 5,644
  • 6
  • 44
  • 71
0

Try this:

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
foreach($your_array as $value){
   fwrite($myfile, $value . '\n');
}
fclose($myfile);
?>
Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16