Sort: Fight fire with fire

Lets just cut to the chase. We have the following array:

$to_sort =  array(
  array("firstname" => "John", "lastname" => "Montgomery"),
  array("firstname" => "Anna", "lastname" => "Belluci"),
  array("firstname" => "Mary", "lastname" => "John"),
);

We need to sort it by last name. sort() wont help… The result array will be sorted by firstname. We can use usort(). But then, why we don’t use sort in our custom sorting function? I think it’s more correct than using lesser than (<) or greater than (>) as string operators (“ala” < "bala").

function sort_me($a, $b)
{
  $test = $mark = array($a['lastname'], $b['lastname']);
  sort($test);
 
  if ($mark != $test)
    return 1;
  return -1;
}
 
usort($to_sort, 'sort_me');

And the result:

Array
(
    [0] => Array
        (
            [firstname] => Anna
            [lastname] => Belluci
        )
 
    [1] => Array
        (
            [firstname] => Mary
            [lastname] => John
        )
 
    [2] => Array
        (
            [firstname] => John
            [lastname] => Montgomery
        )
 
)

The same as when lesser/greater than operators were used.

GMail – getting smarter

Acest articol a fost publicat original aici: GMail – getting smarter

Since we had some problems with blackwp.com (ddos attacks & ISP issues) I turned my attention towards other things that makes the world spin. PHP knowledge level:”PHP?! What is that?” [Beginner - 1/5]

Linux knowledge level:”Yuck! This isn’t Windows!” [Beginner - 1/5]

I used GMail a lot, as I established new contacts and found new ...

Black WP Down!

Acest articol a fost publicat original aici: Black WP Down!

We have some serious hardware problems and our data center was down for almost 2 weeks. Right, less then 2 weeks but we will be down again for like 10 hours when moving to the new data center, right now we are on emergency mode. Sorry once again! ...

WordPress participates on GSoC. Again.

Acest articol a fost publicat original aici: WordPress participates on GSoC. Again.

WordPress participates again on Google Summer of Code program. For that who does not know, Google Summer of Code (aka GSoC) “is a global program that offers student developers stipends to write code for various open source software projects”. Planet WordPress mentions: We’ve been accepted as a mentoring organization again this year, and are looking forward to working ...

Latest Tweets Tooltip

Acest articol a fost publicat original aici: Latest Tweets Tooltip

I found another plugin that will enrich your blog: Latest Tweets Tooltip: A WordPress plugin which will allow you to show the latest tweets about a certain word or phrase in a draggable and resizable jQuery tooltip window. PHP knowledge level:”WordPress is written PHP… Aha…” [Novice - 2/5]

Linux knowledge level:”Yuck! This isn’t Windows!” [Beginner ...