Reading from a flatfile... I need to display array elements sorted by DESCENDING date (= most current date displays first).
Not sure... Is there a way to simply sort the data by element[1] or do I need to put this data into a second array and then sort that array... or ??
Then... PLEASE include the print section... the variables necessary to output the data. Thanks.
(*Yes, I have seen some other examples of this on 'stack.' However, I have not been able to output the values correctly when using the 'compare.')
RAW DATA:
yes|2012-12-12|Jim None
yes|2013-06-04|Joe Smith
no|2013-04-21|Jane Doe
SORTED DATA:
yes|2013-06-04|Joe Smith
no|2013-04-21|Jane Doe
yes|2012-12-12|Jim None
while (!feof($file) ) {
$lines = fgets($file);
$ele = explode('|', $lines);
$db_display = $ele[0];
$db_date = $ele[1];
$db_name = $ele[2];
$db_name = substr_replace($db_name,"",-1); // trim last char
echo '<td>'.$db_name.'</td><td>'.$db_date.'</td>';
}
This should get you there...
Assuming you are getting from a file name $infile
$fp = fopen($infile, "w");
$data = array();
while ($rec = fgetcsv($fp, 2048, "|")){
$data[] = $rec;
}
usort($data, function ($a, $b){
// if you want to reverse the sort swap a for b
return strtotime($b[1]) - strtotime($a[1]);
});
foreach($data as $v){
echo implode("|", $v)."\n";
}
Ouput to standard out ... you can just as easily fopen/fputcsv the same data.