Complicated Array Sorting Please Help Me

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
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Complicated Array Sorting Please Help Me

Post by kaisellgren »

Hi,

Let's get to it...

My array looks like:

Code: Select all

array(1) {
  [1]=>
  array(10) {
    [0]=>
    string(1) "1"
    [1]=>
    string(4) "1985"
    [2]=>
    array(3) {
      [0]=>
      string(320) "Today I'm going to make you feel silly, hypocritical and borderline retarded! Let's get started. While I hope you're not too offended by this post... I do hope you'll change your ways. Before we dig too deep let's get down and do some arithmetic. I'm sure you did this back in Elementary school -- that is if you got pas"
      [1]=>
      string(66) "...and borderline retarded! Let's get started. While I hope you're"
      [2]=>
      string(66) "too offended by this post... I do hope you'll change your ways...."
    }
    [3]=>
    array(3) {
      [0]=>
      string(403) "hat is if you got past pre-school. Before starting on your homework -- get out a pen, a piece of paper and jot down the following: Pay per hour: Pay per day: Pay per month: Amount hourly: Job: Ready? Not yet? Come on Jonny don't hold up the rest of the class! All right.. now let's really get started. Today we're going to teach you a few lesssons. The first lesson we're going to teach you is: How much"
      [1]=>
      string(66) "...er hour: Pay per day: Pay per month: Amount hourly: Job: Ready?"
      [2]=>
      string(66) "yet? Come on Jonny don't hold up the rest of the class! All rig..."
    }
    [4]=>
    array(3) {
      [0]=>
      string(403) "g of this -- you should be a math teacher! 3. So now you have both pay per hour, pay per day and pay per month (X) filled in. You now know what you currently pay your host per hour for your package - not including expenses (support, electricity, their office, phones, servers, bandwidth, licensing fees and other expenses, and many more). 4. Now let's fill in the amount YOU make per hour in 'Amount hou"
      [1]=>
      string(66) "...ow what you currently pay your host per hour for your package -"
      [2]=>
      string(66) "including expenses (support, electricity, their office, phones,..."
    }
  }
}
I am confused how I can sort the array by the year value.

My array consits of sentences. Each sentence is split to 'left', 'right' and 'full' parts. Every text has its ID and Year value too. I need to sort by the year value and in my example array they all have same year 1985.

My array is in a form of:

Code: Select all

array(array(id,year,array(full,left,right)),array(id,year,array(full,left,right)),array(id,year,array(full,left,right)),array(id,year,array(full,left,right));
I'll give reputation and perhaps a donation :)
GloriousEremite
Forum Newbie
Posts: 13
Joined: Tue Aug 14, 2007 1:00 pm

Post by GloriousEremite »

Maybe usort would work, something like:

Code: Select all

<?php
function cmp($a, $b)
{
  if ($a[1]==$b[1])
    return 0;
  return ($a[1]  < $b[1]) ? -1 : 1;
}

//...
usort($array, "cmp");

?>


Untested.
kostyai
Forum Newbie
Posts: 3
Joined: Thu Sep 06, 2007 1:57 pm

Post by kostyai »

if you get that data from a database use ORDER BY clause, it's more effective...
Post Reply