Usort or Sorting a Multidimensional Array
Posted: Mon Mar 03, 2008 5:20 pm
I don't understand the description on php.net for usort. I am trying to sort a list of domains into alphabetical order, but what is throwing me off is the www. part. I don't want the www. part to be included in the alphabetical sort.
This is what I have so far. Hopefully it's a start in the right direction.
I don't know if the "if" statements are the right way to go, but what I need is some type of array that will look like this:
[3] => air.com
[1] => http://www.apple.com
[0] => google.com
[4] => http://www.green.com
[2] => ryan.com
NOTE: The formatting this forum uses is adding the http prefix onto [1] and [4] in the list above. I just want www[.]apple.com and www[.]green.com without http in front of it.
This is what I have so far. Hopefully it's a start in the right direction.
Code: Select all
// id is simply a number, and url will be these:
// http://google.com/
// http://www.apple.com/
// http://ryan.com/
// http://air.com/
// http://www.green.com/
$query = mysql_query("SELECT id, url FROM websites");
while($r = mysql_fetch_array($query)) {
$url = $r["url"];
$url = substr_replace(substr_replace($url, "", -1), "", 0, 7); // removes the http:// and the trailing slash from each url
if(substr($url, 0, 4) == "www.") { // checks for 'www.'
$sites[$r["id"]] = array(substr_replace($url, "", 0, 4), "www.");
} else {
$sites[$r["id"]] = array($url, "");
}
}[3] => air.com
[1] => http://www.apple.com
[0] => google.com
[4] => http://www.green.com
[2] => ryan.com
NOTE: The formatting this forum uses is adding the http prefix onto [1] and [4] in the list above. I just want www[.]apple.com and www[.]green.com without http in front of it.