Jump to content

sorting an associative array


Buchead

Recommended Posts

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

Solution 1

[code]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` [/code]

Solution 2

[code]$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>';[/code]
Link to comment
Share on other sites

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