Jump to content

Sorting output from flat file db


9999

Recommended Posts

Given this script:

 

<?php
$file = file('info.txt');
$statelist = array (1=> "California", "Florida", "Illinois", "New York", "Texas");
foreach ($file as $line => $data)
{
list($state, $abbrev, $population) = explode('|', trim($data));
for ($x=1; $x<5; $x++) {
    		if ($statelist[$x] == $state) {
		echo $state . ", " . $abbrev . " - " . $population . '<br />';
	}
}	
}
?>

 

and this flat file (info.txt):

 

California|CA|36,756,666
Texas|TX|24,326,974
New York|NY|19,490,297
Florida|FL|18,328,340
Illinois|IL|12,901,563

 

 

This is what is outputted:

California, CA - 36,756,666
Texas, TX - 24,326,974
New York, NY - 19,490,297
Florida, FL - 18,328,340
Illinois, IL - 12,901,563

 

How can I sort the output by the index value of the "statelist array" so get this output:

California, CA - 36,756,666
Florida, FL - 18,328,340
Illinois, IL - 12,901,563
New York, NY - 19,490,297
Texas, TX - 24,326,974

 

Could  ksort($statelist) be implemented?

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/214780-sorting-output-from-flat-file-db/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.