Splitting txt file delimited by tabs into multidimensional associative array - php

I currently am trying to read an input file that looks like this:
annie Tuesday October 7 at 08:32 pm 1 Cappuccino 2.5
It is delimited by tabs. I am trying to read from this file, called orders.txt, and place this is an associative array $array. This is the code I have so far. I have tried several different versions, but no dice.
function read_file() {
$file = "orders.txt";
$array = array();
if(file_exists($file)) {
$myfile = fopen($file, "r");
$data = file_get_contents($file);
$lines = explode("\n", $data);
foreach($lines as $line) {
$splode = explode("\t", $line);
$array[] = array(
"Name" => $splode[0],
"Time" => $splode[1],
"Quant" => $splode[2],
"Type" => $splode[3],
"Price" => $splode[4],
);
}
fclose($myfile);
}
return $array;
}
Could anyone see what I am doing wrong here? Thank you.

Your code looks good. I did add one if statement to make sure splode was 5 long before assigning it. This is protection against a possible blank line at the end of a file.
I ran it on a test file here with a few lines and it processed correctly and outputted as expected.
Depending on how you're creating this file - could you have a '\r' or a '\r\n' on the end of each line instead of just a \n?? This is something you'd need to check - maybe a hex editor, but I still think your code should run ok (unless it's just a \r) as long as there's enough tabs to satisfy the 5 on each line (which I conditionaled for in my suggestion).
function read_file()
{
$file = "orders.txt";
$array = array();
if (file_exists($file)) {
echo "Get";
$myfile = fopen($file, "r");
$data = file_get_contents($file);
$lines = explode("\n", $data);
var_dump($lines);
foreach ($lines as $line) {
$splode = explode("\t", $line);
if (sizeof($splode) >= 5) $array[] = array(
"Name" => $splode[0],
"Time" => $splode[1],
"Quant" => $splode[2],
"Type" => $splode[3],
"Price" => $splode[4],
);
}
fclose($myfile);
}
return $array;
}

Related

how to create array inside array from text file in php

first, i have a text file
Samsung|us|new
iPhone|china|new
i want to convert the text file, and the result must be like this
[
[
'Samsung', 'us', 'new'
],
[
'iPhone', 'china', 'new'
]
]
i have already try this, but the code only return one array
code:
<?php
$a = file_get_contents("text.txt");
$b = explode('|', $a);
result:
[
'Samsung','us','new','iPhone','china','new'
];
According to the hint from Jeto I would do the following:
at first read the file with function file() with the flag FILE_IGNORE_NEW_LINES. This reads the file line by line and creates an array.
next step would be to iterate over each element and split by | character with explode().
This could be the resulting code:
$file = file('test.txt', FILE_IGNORE_NEW_LINES);
for($i = 0; $i < count($file); $i++)
{
$file[$i] = explode('|', $file[$i]);
}
This is because file_get_contents() reads the whole file including the line breaks.
You have to first explode() on \n. After that explode() on |.
Or with array_map() in one line:
$a = file_get_contents("text.txt");
$b = array_map(fn($line) => explode('|', $line), explode("\n", $a));
// $a with \n
// this explode splits the lines
// this explodes at the | character
Example: https://3v4l.org/24qla
If you want to read some big files you can use something like this:
function getCsvData($file, $delimiter = '|') {
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
yield $data;
}
fclose($handle);
}
}
foreach(getCsvData('test.txt') as $row) {
print_r($row);
}
Don't use file_get_contents open the file and read the file line by line. Then split the line.
https://www.php.net/manual/en/function.fgets.php
Here are some example to do this. You can use fgets for this. With file_get_contents you get the whole file.
Another solution is to explode by \r\n or \n the characters for new line. Then you have the single lines and you can split them by your delimiter. But in this case you write the whole content in an array what can cause some memory problem.
explode("\n",$homepage)

Associative-array in PHP from CSV / TXT file

I have a problem with associative-array in PHP – when the source of the arrays is from a text file.
When I write something as follows:
$logins = array('user1' => '1234','user2' => '2345','user3' => '3456');
It all works as expected.
So, I tried to call those arrays from CSV file like that:
$file_handle = fopen("data.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
if (empty($line_of_text)) { break; }
$logins = array($line_of_text[0] . '=>' . $line_of_text[1]); /* remove the => and seperate the logins with "," on CSV */
}
It didn't work.
There are a lot close related questions and answers here on SO but I did read and try to implant them without no success. Please Guide me.
Edit: data.csv looks like as follows.
user1,1234;
user2,2345;
user3,3456;
You can avoid those loops, conditionals, and fopen()/fclose() messiness:
<?php
// read the file into an array
$arr = file("data.csv", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// split each line at the comma
array_walk($arr, function(&$v, $k){$v=explode(",", $v);});
// build an array from the data
$keys = array_column($arr, 0);
$values = array_column($arr, 1);
$logins = array_combine($keys, $values);
Here is what I think you want
$logins = array();
$file_handle = fopen("data.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
// At this point, $line_of_text is an array, which will look
// something like this: {[0]=>'user1',[1]=>'1234'}
if (empty($line_of_text)) { break; }
$logins[$line_of_text[0]] = $line_of_text[1];
// So the line above is equivalent to something like
// $logins['user1'] = '1234';
}
This would probably also work, though I think it's not something you really want to get into
/* $dataFile = fopen("data.txt", "r"); */
$dataFile = file_get_contents("data.txt");
/* logins = array($dataFile); */
eval('$logins = ' . $dataFile . ';');

PHP write in file in specific location

Imagine I have a TXT file with the following contents:
Hello
How are you
Paris
London
And I want to write beneath Paris, so Paris has the index of 2, and I want to write in 3.
Currently, I have this:
$fileName = 'file.txt';
$lineNumber = 3;
$changeTo = "the changed line\n";
$contents = file($fileName);
$contents[$lineNumber] = $changeTo;
file_put_contents($fileName, implode('',$contents));
But it only modifies the specific line. And I don't want to modify, I want to write a new line and let the others stay where they are.
How can I do this?
Edit: Solved. In a very easy way:
$contents = file($filename);
$contents[2] = $contents[2] . "\n"; // Gives a new line
file_put_contents($filename, implode('',$contents));
$contents = file($filename);
$contents[3] = "Nooooooo!\n";
file_put_contents($filename, implode('',$contents));
You need to parse the contents of the file, place the contents in a new array and when the line number you want comes up, insert the new content into that array. And then save the new contents to a file. Adjusted code below:
$fileName = 'file.txt';
$lineNumber = 3;
$changeTo = "the changed line\n";
$contents = file($fileName);
$new_contents = array();
foreach ($contents as $key => $value) {
$new_contents[] = $value;
if ($key == $lineNumber) {
$new_contents[] = $changeTo;
}
}
file_put_contents($fileName, implode('',$new_contents));

Convert delimited strings into an associative array

I have a file codes.txt with records like this
USA 0233
JPN 6789
TUN 8990
CDN 2345
I want to read these content of the file to an associative array like this.
$codes["USA"] = 0233;
$codes["JPN"] = 6789;
$codes["TUN"] = 8990;
$codes["CDN"] = 2345;
this is the code that opens the file for writing. I need help in the array part. thks
$myFile = "codes.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh)
It's pretty straight forward. First read the file line-by-line (e.g. see the file function which does this already). Then parse each line by splitting it at the first space (see explode), use the first part as key and the second part as value:
$array = array();
foreach (file($myFile) as $line)
{
list($key, $value) = explode(' ', $line, 2) + array(NULL, NULL);
if ($value !== NULL)
{
$array[$key] = $value;
}
}
If your parser needs to be more specific, change it according to your needs. There are a lot of string functions with PHP that can be used for more differentiated parsing, like sscanf or regular expressions.
Another common method is to extend from SplFileObject, it allows to encapsulate the data-aquisition inside an iterator so you can better differentiate between the place where the data is used and where it is taken from. You can find an example in another answer:
PHP - Process CSV Into Array With Column Headings For Key
$myFile = "codes.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
$assoc_array = array();
$my_array = explode("\n", $theData);
foreach($my_array as $line)
{
$tmp = explode(" ", $line);
$assoc_array[$tmp[0]] = $tmp[1];
}
fclose($fh);
// well the op wants the results to be in $codes
$codes = $assoc_array;
There is an optimal way of doing this than any of these answers. You can use file_get_contents and array_walk functions to cut the things very short.
$allCases = explode("\r\n", file_get_contents("myfile.txt"));
$myList = array();
array_walk($allCases, "step", $myList);
function step($val, $key, &$arr) {
$aCase = explode(" ", $val);
$arr[$aCase[0]] = $aCase[1];
}
This way you don't need a for loop or a file handler.
Beware of the delimiter "\r\n" since this could be platform dependent in this case.

How to replace one line in php?

I have test.txt file, like this,
AA=1
BB=2
CC=3
Now I wanna find "BB=" and replace it as BB=5, like this,
AA=1
BB=5
CC=3
How do I do this?
Thanks.
<?php
$file = "data.txt";
$fp = fopen($file, "r");
while(!feof($fp)) {
$data = fgets($fp, 1024);
// You have the data in $data, you can write replace logic
Replace Logic function
$data will store the final value
// Write back the data to the same file
$Handle = fopen($File, 'w');
fwrite($Handle, $data);
echo "$data <br>";
}
fclose($fp);
?>
The above peace of code will give you data from the file and helps you to write the data back to the file.
Assuming that your file is structured like an INI file (i.e. key=value), you could use parse_ini_file and do something like this:
<?php
$filename = 'file.txt';
// Parse the file assuming it's structured as an INI file.
// http://php.net/manual/en/function.parse-ini-file.php
$data = parse_ini_file($filename);
// Array of values to replace.
$replace_with = array(
'BB' => 5
);
// Open the file for writing.
$fh = fopen($filename, 'w');
// Loop through the data.
foreach ( $data as $key => $value )
{
// If a value exists that should replace the current one, use it.
if ( ! empty($replace_with[$key]) )
$value = $replace_with[$key];
// Write to the file.
fwrite($fh, "{$key}={$value}" . PHP_EOL);
}
// Close the file handle.
fclose($fh);
The simplest way (if you are talking about a small file as above), would be something like:
// Read the file in as an array of lines
$fileData = file('test.txt');
$newArray = array();
foreach($fileData as $line) {
// find the line that starts with BB= and change it to BB=5
if (substr($line, 0, 3) == 'BB=')) {
$line = 'BB=5';
}
$newArray[] = $line;
}
// Overwrite test.txt
$fp = fopen('test.txt', 'w');
fwrite($fp, implode("\n",$newArray));
fclose($fp);
(something like that)
You can use Pear package for find & replace text in a file .
For more information read
http://www.codediesel.com/php/search-replace-in-files-using-php/

Categories