Jump to content


Photo

sorting an associative array


  • Please log in to reply
4 replies to this topic

#1 Buchead

Buchead
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 06 May 2006 - 01:48 PM

Hello....

I have an array that consists of multiple class items. The number of items depends upon what's read in from the database. I'm having trouble sorting the array based on one particular class item.

The class item consists of personal information (name, address, time of service, dept) and the array is populated depending upon which dept is required. I'd like to have the output then sorted by time of service. I can get it displaying fine as in the order it's read from the database and stored in the array.

Due to the information being in different tables I can't find a way to simple sort it using a mySql command.

I've tried creating a new array and populating it each of the original array using the time of service as a key. But that doesn't work, and I'd think it's a very bad route to take. I "think" I know how to solve it but just know the commands!

Thanks for any help,

Clive.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 May 2006 - 10:18 AM

So there are two possible solution

- order it in the original query, or
- sort the array

For the first, we need to know the layouts of the tables.

For the second we need to know the array structure. If you use var_export($arrayname) then post the output here it makes it easy to recreate the array locally.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Buchead

Buchead
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 07 May 2006 - 10:39 AM

The tables (5 of them) all have the same structure:

Name - varchar(35)
Address - varchar(200)
Service - tinyint
Dept - char(10)

I use the following to obtain a list of all the tables:

$tbls = @mysql_query("SHOW TABLES");

A loop then goes through retrieving the data and populating the array:-

$result=@mysql_query("SELECT * FROM `$tbl` WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') ORDER BY `Name`,`Service`",$link);

I'm unsure how to get this command to put the output into the correct order as it's simply looping through the tables, rather
than having the ability to jump between tables.



Where about should I put the var_export as it throws up an error wherever I seem to put it.


Thanks.

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 May 2006 - 01:20 PM

I can't resist asking why 5 identically structured tables instead of 1?

Solution 1

SELECT * FROM table1 
    WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') 
UNION
SELECT * FROM table2 
    WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') 
UNION
SELECT * FROM table3 
    WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') 
UNION
SELECT * FROM table4 
    WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') 
UNION
SELECT * FROM table5 
    WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') 
ORDER BY `Service`

Solution 2

$data = array (
      array (
        'Name' =>  'aaa',
        'Address' => 'London',
        'Service' => 30,
        'Dept' => 'Dept A'    
          ),
          
      array (
        'Name' =>  'bbb',
        'Address' => 'London',
        'Service' => 10,
        'Dept' => 'Dept B'    
          ),
          
      array (
        'Name' =>  'ccc',
        'Address' => 'London',
        'Service' => 20,
        'Dept' => 'Dept C'    
          )        
);

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

usort($data, 'service_sort');

// check sort result
echo '<pre>', print_r($data, true), '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Buchead

Buchead
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 07 May 2006 - 03:05 PM

Thanks for that - gives me somewhere to start.

Unfortunately I didn't create the database. It's been long-existant in work and they won't let me change it. And my manager, like all managers, thinks it's easy to get the desired results! Though I guess it is if you've used php before.


Thanks again.........




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users