Comma Delimited file

I have a comma delimited text file with several records. A typical record looks like this:

Jim,Smith,1234 Main Street,Anytown,NY,12345,206,123-3456

I need to read the file andd then output the data (name and phone number) in a two columm table like this:

Smith, Jim (206) 123-3456

The data also needs to be sorted by last name.

I assume I will be using an array, but I’m not sure how to do it.

If you can help me, email me at [email protected].

Thanks,

Andrew

Assuming each line in your text file (data.txt) present one record, the code for your task can be written as follows:

[php]

<?php $arr_names = array(); $arr_phones = array(); // reading data file into array $records = file('data.txt'); // parsing lines read from data file and populate arrays with names & phones if(is_array($records)) foreach($records as $line){ $line = trim($line); if($line!=''){ $fields = explode(',',$line); $arr_names[] = $fields[1].', '.$fields[0]; $arr_phones[] = '('.$fields[6].') '.$fields[7]; } } // sorting by last, first name, maintaining key association in both arrays array_multisort($arr_names, $arr_phones); // displaying names & phones in two column table echo ''; echo ''; foreach($arr_names as $key => $name){ echo ''; } echo '
Last, First Name Phone
'.htmlspecialchars($name).' '.htmlspecialchars($arr_phones[$key]).'
'; ?>

[/php]

Thanks !!!

Andrew

Sponsor our Newsletter | Privacy Policy | Terms of Service