Usort or Sorting a Multidimensional Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryanfait
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2008 5:06 pm

Usort or Sorting a Multidimensional Array

Post by ryanfait »

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.

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, "");
    }
}
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.
User avatar
hawkenterprises
Forum Commoner
Posts: 54
Joined: Thu Feb 28, 2008 9:56 pm
Location: gresham,oregon
Contact:

Re: Usort or Sorting a Multidimensional Array

Post by hawkenterprises »

I just posted the wonderful function called parse_url on another topic

http://www.php.net/parse_url

It gives you this

(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)


then all you have to do is something like this

Code: Select all

 
$url = 'www.blah.com';
$url_parts = parse_url($url);
$domain_parts = explode('.',$url_parts['host']);
 
var_dump($domain_parts);
 
This should get you closer to a solution.
ryanfait
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2008 5:06 pm

Re: Usort or Sorting a Multidimensional Array

Post by ryanfait »

Yeah, I was playing with parse_url before, but how can I sort it when some parts will be ['host'][0] and others will be ['host'][1]?
User avatar
hawkenterprises
Forum Commoner
Posts: 54
Joined: Thu Feb 28, 2008 9:56 pm
Location: gresham,oregon
Contact:

Re: Usort or Sorting a Multidimensional Array

Post by hawkenterprises »

Well the idea is that you will have to build the array in away it can be sorted. By using explode you will have something that looks like this

$parts => array( 0 => 'www', 1 => 'domain', 2 => 'com');

Then just grab $parts[1] and you should have a domain, I would do a check just to be certain, use strstr function.
Try working with that information and forming the array differently until you have what you need.
Post Reply