Hello helpers,

I'm having a problem with sorting (3rd column) a flat file.

flat file looks like this:
20210103|~|3 jan 2021|~|Harold|~|m|
20210121|~|21 jan 2021|~|Elly|~|f|
20210213|~|13 feb 2021|~|Silvy|~|f|
20210330|~|30 mrt 2021|~| George|~|m|

Now I want to save it in a new flat file sorted on the 3rd column
Result:
20210121|~|21 jan 2021|~|Elly|~|f|
20210330|~|30 mrt 2021|~|George|~|m|
20210103|~|3 jan 2021|~|Harold|~|m|
20210213|~|13 feb 2021|~|Silvy|~|f|

I already have a code for making new flat file but I'm stuck on sorting 3rd column, The sorting does not work.
Can somebody help me by adding the code for sorting correctly please?

Code I already have:

<?php
$data = trim(file_get_contents('../myfile.dat'));
$data = explode("\n", $data);
$array_order = array();
$fileLocation = getenv("DOCUMENT_ROOT") . "/mynewfile.txt";
$file = fopen($fileLocation, "w");
for ($i = 0; $i < count($data); $i++) {
($i > 0) ? $array_order[$i] = $data[$i] : fwrite($file, $data[0]);
}
sort($array_order);
$data = implode("\n", $array_order);
fwrite($file, $data);
fclose($file);
echo 'file created - location::' . $fileLocation;
?>

Thanks in advance
Kind regards.

Recommended Answers

What do you mean when you say it doesn’t work? Do you get an error message? Does it sort in the wrong order? What happens after you call that sort() function?

Jump to Post

All 3 Replies

What do you mean when you say it doesn’t work? Do you get an error message? Does it sort in the wrong order? What happens after you call that sort() function?

Thanks for your reply.
No, no errors but it sort on the first column.
I want it to sort on the third column ... (bold names)

Nevermind.
I found it myself.

This post can be deleted!
Thanks for reading and support.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.