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
https://forums.phpfreaks.com/topic/94460-usortsorting-a-multidimensional-array/
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>";
          
        }
          }
         
          ?>

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

*/

Archived

This topic is now archived and is closed to further replies.

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