Reputation: 19
Ive been trying to do this for weeks and i have been looking and trying lots of different ways with previously asked questions.
I have a text (txt) file which contains data on people like this: ID:NAME.SURNAME:AGE
1:hannah.smith:20:
2:bob.jones:40:
3:james.williams:19:
4:ben.higgins:25:
100:andrew.ross:70:
....
The file has about 150 lines of names in that format.
What i want to do is read from the file and separate each 'field' so they are all seperate(ID, NAME, SURNAME, AGE). I want to sort the data in alphabetical order from the first name. So:
100:andrew.ross:70:
4:ben.higgins:25:
2:bob.jones:40:
1:hannah.smith:20:
3:james.williams:19:
....
I have tried using some of php's CSV file but i end up with lots of nested arrays. i tried this and this kinda works but i dont know how to sort it. If there is a better way of doing all of this then i would greatly appreciate it. But so far i have read the file but not sure how to go about sorting it in alphabetical order and maybe putting each field in a sorted array?
Heres my code so far:
$file =
file_get_contents("/file_path", 0,
NULL, 148);
array_shift($rows);
foreach($rows as $row => $data)
{
//get row data
$row_data = explode(':', $data);
$info[$row]['id'] = $row_data[0];
$info[$row]['name'] = $row_data[1];
$info[$row]['surname'] = $row_data[2];
$info[$row]['age'] = $row_data[3];
//display data
echo 'Row ' . $row . ' ID: ' . $info[$row]['id'] . "\n";
echo 'Row ' . $row . ' First Name: ' . $info[$row]['name'] . "\n";
echo 'Row ' . $row . ' Last Name: ' . $info[$row]['surname'] . "\n";
echo 'Row ' . $row . ' Age: ' . $info[$row]['age'] . "\n";
}
Upvotes: 0
Views: 306
Reputation: 33823
I think you can accomplish the sorting in a manner such as this, though in all honesty a database would be a much better solution.
The below uses an anonymous callback function as the second argument to usort
which allows us to set whatever rules we wish for the sorting.
$file=__DIR__ . '/sort-target.txt';
$lines=file( $file, FILE_SKIP_EMPTY_LINES | FILE_TEXT | FILE_IGNORE_NEW_LINES );
usort( $lines, function( $a, $b ){
list( $id_a, $name_a, $age_a )=array_filter( explode(':',$a) );
list( $id_b, $name_b, $age_b )=array_filter( explode(':',$b) );
return $name_a > $name_b;
} );
printf('<pre>%s</pre>', print_r( $lines, true ) );
The contents of the source file is:
1:hannah.smith:20:
2:bob.jones:40:
3:james.williams:19:
4:ben.higgins:25:
100:andrew.ross:70:
The output is:
Array
(
[0] => 100:andrew.ross:70:
[1] => 4:ben.higgins:25:
[2] => 2:bob.jones:40:
[3] => 1:hannah.smith:20:
[4] => 3:james.williams:19:
)
Upvotes: 0
Reputation: 32
Hi you could separate your data into arrays for further use and then alphabetical order hopefully that helped.
// checking if file exists
if(file_exists('file.txt')){
//open the file and get ready for reading Data
//make sure your file has permission on your server to read the file
$handle = fopen('files.txt', 'r');
if($handle) {
$Id = array();
$email = array();
$order = array();
while(!feof($handle)){
$data = explode(':', fgets($handle, 1024), 3);
//store data in your array for futher use
$Id[] = $data[0];
$email[] = $data[1];
$order[] = $data[2];
}
//sort array alphabetical order
sort($email);
foreach ($email as $emails) {
echo '<pre>';
echo print_r($emails);
echo '<pre>';
}
}else{
echo 'error openning file!';
}
}else{
echo '<p>File not found!</p>';
}
Upvotes: 0
Reputation: 11
It seems like you're over complicating it, all you really need to do is split the file by the line (file() already does this) then explode() each line by the : character, push the results into an array of arrays, then do an array sort on the result, like this;
$a = Array(
1 => Array(
0 => 'Peter',
1 => 17
),
0 => Array(
0 => 'Nina',
1 => 21
),
2 => Array(
0 => 'Bill',
1 => 15
),
);
function compareByName($a, $b) {
return strcmp($a[0], $b[0]);
}
usort($a, 'compareByName');
print_r($a);
After this, you just iterate and do whatever you want with the content, like maybe output to csv or something.
Upvotes: 1