TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-30-2011, 01:37 PM   #1 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default Array Sorting !

Am at my wits end here and am hoping there is a quick and easy solution....

I need to sort a multi dimentional array, the array is built as follows (abridged):
Code:
$i++;
$missed[$i] = array($row[3], $row[4], ($weeks - $num_rows));
so for example the data would look like :
Code:
$missed[0] = array(Fred, Smith, 3)
$missed[1] = array(Bert, Smith, 99)
$missed[2] = array(Gerty, Blogs, 22)
My problem is, how do I sort the array $missed on the last number in the array ?

Or, am I going about this in the wrong way ?
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 07-30-2011, 02:15 PM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

Got it sorted... Bit more googling and found the following code..

Code:
function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 08-01-2011, 04:38 AM   #3 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

what about usort:

PHP Code:
usort($missed, function ($a, $b) {
  if ($a[2] == $b[2]) {
    return 0;
  }
  return ($a[2] < $b[2]) ? -1 : 1;
});
tony is offline  
Reply With Quote
Old 08-01-2011, 03:48 PM   #4 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

Thanks Tony, thats one for the "code snippets" :D

Ok, next problem....

Code:
$missed[0] = array(Fred, Smith, 3)
$missed[1] = array(Bert, Smith, 52)
$missed[2] = array(Gerty, Blogs, 22)
$missed[3] = array(Fred, Blogs, 22)
How do I count the quantity of unique occurances of the last emement this the array, i.e.

Code:
$uniques[$i] = array("missed" => 22, "occurances" => 2)
There are around 35k records that I have to go through, so not enormous, and there can only be a max of 52 unique entries (by design), I'm just getting lost how to itterate through the arrays and count them correctly.. Any ideas ?
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 08-01-2011, 07:59 PM   #5 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

you can use the the hash-table characteristic of arrays in your advantage here. For example:
PHP Code:
$uniques = array();
foreach($missed as $record) {
  $uniques[$record[2]] = empty($unique[$record[2]]) ? 0 : ($unique[$record[2] + 1);
}
At the en unique would have the the "missed" part as the key and the "occurrences" as the value in your example it would be like this:

PHP Code:
$uniques[22] = 2;
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
maeltar (08-02-2011)
Old 08-02-2011, 05:27 AM   #6 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

You sir, are a god :D
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 08-02-2011, 12:51 PM   #7 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

Glad it helped. I am not a god haha, if anything a sorcerer's apprentice :P
tony is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting array of folder list Peuplarchie General 1 12-16-2009 01:42 AM
Sorting an Array Hightower General 5 08-25-2009 06:26 PM
How to search array benton General 3 03-29-2009 12:31 AM
self submited form problem, sorting, cleaning and array... Peuplarchie Advanced PHP Programming 3 11-11-2008 12:08 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 03:05 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design