When creating algorithms where you don't instinctively know what to do, put your problem ito words, and start breaking it down, still using words.
I want an associative array of people from this file, doesn't help much, but you know some things about the data (that byt the way the rest of us don't, so I'm purely guessing):
Each line in the file contains a person.
Each person is described by CSV, where the 1st is fname, 2nd is lname etc.
So:
1. Get data for all people
2. Build an array from 1.
3. Sort array
Might change into...
1. Build an array of people by...
1. get data for one person
2. add to result array
3. repeat 1.1 until no more people
2. Sort array
And then you break down some things into smaller parts
1.1 Get data for one person
1. read line from file
2. parse the different parts of the line to get: fname, lname, acode, phone
3. store 1.2 as an array
1. [this part is not included in my example code skeleton below]
make this array associative with keys 'lastName', 'firstName' etc
You wanted to sort by last name, but you may realize that you also need to sort by first name to avoid things like:
Davis John
Davis Joe
So you split the sorting into
2.1 Sort by lname
2.2 sort by fname
But later down the line when you start replacing parts with actual code, you can even reduce several steps into one step sometimes.
2. Sort array
1. Sort by lname and fname => array_multisort
I think what you want is
fopen( , 'r');
while(fgets()) {
str_getcsv();
# associative array representing a person should be dealt with here
}
fclose();
array_multisort();
Complete the blanks to have a working script. Then you can add code to change 0, 1, 2, 3 into 'firstName', 'lastName', 'areaCode' and 'phone' by adding more code where the comment is.