Jump to content

usort/Sorting a multidimensional array


ryanfait

Recommended Posts

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. to be included in the alphabetical sort.

 

This is what I have so far. Hopefully it's a start in the right direction.

 

 
// 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] => www.apple.com

[0] => google.com

[4] => www.green.com

[2] => ryan.com

 

Link to comment
Share on other sites

have a look at this example just done............



<?php

$a=array("http://www.google.com",
         "http://www.apple.com",
         "http://www.ryan.com",
         "http://www.air.com",
         "http://www.green.com");
         
         asort($a);
         
        foreach($a as $url){
        	
        if( eregi("^(http://www.)",$url)){
         	
         $a=str_replace("http://www."," ",$url);
         
         $b="http://www.";
         
         $result=$b.$a;
         
         echo "<br><a href='$url'>$url</a><br>";
          
        }
          }
         
          ?>

Link to comment
Share on other sites

Thanks for the time, but I explained what I'm trying to do poorly. This is more to the point of exactly where I am stuck.

 

I'll show by example and hopefully it'll make more sense.

 

 

$urls = array();

$urls["ryan"] = ryan.com;
$urls["ryan"] = apple.com;
$urls["ryan"] = green.com;
$urls["ryan"] = www.cool.com;
$urls["julia"] = this.com;
$urls["julia"] = that.com;
$urls["julia"] = www.here.com;
$urls["julia"] = www.another.org;
$urls["julia"] = table.com;
$urls["julia"] = cherry.com;
$urls["megan"] = www.london.com;
$urls["megan"] = bed.com;
$urls["megan"] = www.chair.com;

fancy_sort_function($urls);

foreach($urls as $name => $url) {
echo $url" -- ".$name."<br />";
}

/*

The foreach above I want to print out:

www.another.org -- julia
apple.com -- ryan
bed.com -- megan
www.chair.com -- megan
cherry.com -- julia
www.cool.com -- ryan
green.com -- ryan
www.here.com -- julia
www.london.com -- megan
ryan.com -- ryan
table.com -- julia
that.com -- julia
this.com -- julia

*/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.