Re: [PHP] Array Sorting
On Thu, May 4, 2006 10:20 am, James Nunnerley wrote: What I'm not sure about doing is sorting by say Size or Date? There's several different examples of date sorts here: http://www.weberdev.com/AdvancedSearch.php?searchtype=titlesearch=date+sortSubmit1.x=0Submit1.y=0 HTH's tedd -- http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Array Sorting
Hi All, I've got an array which has the following properties: $file_array[$filename] = array (Date = $Date, size = $size, permissions = $permissions); I can quite happily sort the array by filename (using natksort and natkrsort), which I found on the php manual - and for reference have included as a PS What I'm not sure about doing is sorting by say Size or Date? I've again had a look through the php manual but I'm not sure how to sort by a property... Can anyone point me in a direction? Cheers Nunners PS as promised! function natksort($aToBeSorted) { $aResult = array(); $aKeys = array_keys($aToBeSorted); natcasesort($aKeys); foreach ($aKeys as $sKey) { $aResult[$sKey] = $aToBeSorted[$sKey]; } $aToBeSorted = $aResult; return True; } function natkrsort($aToBeSorted) { $aResult = array(); $aKeys = array_keys($aToBeSorted); $aKeys = array_reverse(natcasesort($aKeys)); foreach ($aKeys as $sKey) { $aResult[$sKey] = $aToBeSorted[$sKey]; } $aToBeSorted = $aResult; return True; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting
James Nunnerley wrote: I've got an array which has the following properties: $file_array[$filename] = array (Date = $Date, size = $size, permissions = $permissions); I can quite happily sort the array by filename (using natksort and natkrsort), which I found on the php manual - and for reference have included as a PS What I'm not sure about doing is sorting by say Size or Date? I've again had a look through the php manual but I'm not sure how to sort by a property... Can anyone point me in a direction? http://php.net/usort -- it be thataway! -Stut PS. Please start a new thread instead of just replying to someone elses and changing the subject - it screws up the threading in decent email clients. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Array Sorting
I think I may have written down my variable structure incorrectly in the first place, which even has me confused... The $file_array[$filename] is I don't think a further array. The sub values are created as follows: $file_array[$filename][Date] = ... $file_array[$filename][Size] = ... Looking at usort, it will put the list of Dates in order (by doing a pure if is bigger or smaller...) but will loose the link between the particular filename and it's date properties. Does that make sense - or am I loosing the plot? Am I also making a mistake by not defining $file_array[$filename]? I don't think so, but would be happy to be corrected! Cheers Nunners -Original Message- From: Stut [mailto:[EMAIL PROTECTED] Sent: 04 May 2006 16:24 To: James Nunnerley Cc: php-general@lists.php.net Subject: Re: [PHP] Array Sorting James Nunnerley wrote: I've got an array which has the following properties: $file_array[$filename] = array (Date = $Date, size = $size, permissions = $permissions); I can quite happily sort the array by filename (using natksort and natkrsort), which I found on the php manual - and for reference have included as a PS What I'm not sure about doing is sorting by say Size or Date? I've again had a look through the php manual but I'm not sure how to sort by a property... Can anyone point me in a direction? http://php.net/usort -- it be thataway! -Stut PS. Please start a new thread instead of just replying to someone elses and changing the subject - it screws up the threading in decent email clients. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting
James Nunnerley wrote: I think I may have written down my variable structure incorrectly in the first place, which even has me confused... The $file_array[$filename] is I don't think a further array. The sub values are created as follows: $file_array[$filename][Date] = ... $file_array[$filename][Size] = ... Looking at usort, it will put the list of Dates in order (by doing a pure if is bigger or smaller...) but will loose the link between the particular filename and it's date properties. Does that make sense - or am I loosing the plot? That is indeed what it will do. If that's a problem you probably also want to define a Filename element for each item... $file_array[$filename][Filename] = $filename; It's worth noting that if you're not using the fact that they're indexed on the filename it's probably faster to use integer keys instead of strings, but don't quote me on that. I'm not overly familiar with the internals of PHP... yet! Am I also making a mistake by not defining $file_array[$filename]? I don't think so, but would be happy to be corrected! PHP is quite relaxed about these things, but from syntactic(?) completeness and security points of view it should really be initialised to an empty array. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting
On Thu, May 4, 2006 10:20 am, James Nunnerley wrote: $file_array[$filename] = array (Date = $Date, size = $size, permissions = $permissions); I can quite happily sort the array by filename (using natksort and natkrsort), which I found on the php manual - and for reference have included as a PS What I'm not sure about doing is sorting by say Size or Date? If size is an integer, then, like, just plain old 'sort' should do it, though it's up to you to figure out the multi-dimensional bit. For dates, there are several possiblities... If you Date happens to be Unix timestamp, it's an integer, and is solved by the above. If your Date happens to be in a format not unlike 'Y/M/D' it can be sorted with plain old sort as a string. In situations where Date is in a more natural format, you can use http://php.net/usort and define your own comparison function any which way you want. All that aside, if, like most, this data is coming from the database, you should just use your SQL query to sort the data, if possible, as it will be MUCH more efficient. YMMV -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting
On Thu, May 4, 2006 10:47 am, Stut wrote: It's worth noting that if you're not using the fact that they're indexed on the filename it's probably faster to use integer keys instead of strings, but don't quote me on that. I'm not overly familiar with the internals of PHP... yet! I think this qualifies as an Urban Legend. :-) Could be wrong, but you are welcome to benchmark it, and not post results, because the results are already in the archives. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array sorting
Hi there, I would like to save some db power by putting values into a file which are often used. They basicly populate a select field. So I placed those values into associative arrays: $code[language] = array(1= php, 2= asp); Now I would like to sort those for displaying after my preference, not by lets say alphabet. The easiest thing I could think of, is just to move the entry: $code[language] = array(2= asp, 1= php); But this of course does not work. I would like to be able to place values later on inbetween. So if somebody comes up with a language called .net :-) I would like to place it in the middle, but I cant get it to work like that :-( To build the select box I use following code: select name=education option value =0 selected---/option '; # build the select field for ($i=1; $i = count($code[education]); $i++){ if ($education) $isselected = ($i == $education) ? selected : ; // select the active row else $isselected = FALSE; printf (option value =\%s\%s%s/option\n, $i, $isselected, $code[education][$i]); }; echo' /select Has anybody an idea which could help me on that? Thank you for any help. Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Array Sorting Headaches
Greetings everyone : Having a hard time with this one. I have a multi-dim array $foo[$x][$y]['key'], where $x and $y are numeric. Here is some sample data : Array ( [$x] = Array ( [0] = Array ( [invoiceid] = 11842 [product] = myproduct [domain] = foo.net [expires] = February 28, 2004 ) [1] = Array ( [invoiceid] = 10295 [product] = myotherproduct [domain] = foo.net [expires] = December 9, 2003 ) [2] = Array ( [invoiceid] = 10202 [product] = product1 [domain] = foo.bar [expires] = January 19, 2003 ) [3] = Array ( [invoiceid] = 10005 [product] = product2 [domain] = foo.bar [expires] = December 11, 2002 ) ) ) I need to filter the results so that I get the latest expiry date for each product. The expires field actually contains a timestamp. So for the sample array above, the resultant array would have only keys 0 and 2, filtering out all the rest. There are around 180+ main entries, with any number of sub entries, but each sub entry has the same four keys. Any ideas? Getting a rather large headache mulling over this. Thanks again, Burhan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Array Sorting, 2 items...
Hi, Trying to accomplish: I want to sort my array by two columns. The array is setup: Array ( [0] = Array ( [0] = CHECKING [ba_type] = CHECKING [1] = 10132200 [loan_number] = 10132200 [2] = 1049 [loan_id] = 1049 [3] = MONICA [first_name] = MONICA [4] = MERCHANT [last_name] = MERCHANT [5] = 2003-07-17 16:15:32 [approved_date] = 2003-07-17 16:15:32 [6] = ACH Debit [type] = ACH Debit ) [1] = Array ( [0] = CHECKING [ba_type] = CHECKING [1] = 10154654 [loan_number] = 10154654 [2] = 1055 [loan_id] = 1055 [3] = RICH [first_name] = RICH [4] = LABO [last_name] = LABO [5] = 2003-07-17 16:15:32 [approved_date] = 2003-07-17 16:15:32 [6] = ACH Debit [type] = ACH Debit ) ) What I'd like to happen is first sort by ba_type, and then sort by loan_number, so that I end up with a nice output similar to; CHECKING 101234 CHECKING 101544 CHECKING 101573 SAVINGS 101112 SAVINGS 101224 VISA 101110 VISA 101998 Keep everything groupped together in their respective ba_type, and put the numbers in numeric order. I've looked at usort, and many examples of it. I can see how to use it to sort one column, but how would I sort a second to achieve this? -Dan Joseph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting, 2 items...
Your array seems like a result of mysql_fetch_array(), cannot you order it in the sql query? Dan Joseph wrote: Array ( [0] = Array ( [0] = CHECKING [ba_type] = CHECKING [1] = 10132200 [loan_number] = 10132200 Keep everything groupped together in their respective ba_type, and put the numbers in numeric order. I've looked at usort, and many examples of it. I can see how to use it to sort one column, but how would I sort a second to achieve this? Simply if ba_types are equal, continue with checking loan_numbers: function cmp ($a, $b) { if ($a['ba_type'] == $b['ba_type']) { if ($a['loan_number'] == $b['loan_number']) return 0; return ($a['loan_number'] $b['loan_number']) ? -1 : 1; } return ($a['ba_type'] $b['ba_type']) ? -1 : 1; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Array Sorting, 2 items...
Hi, Yeah, I did get it from a mysql_fetch_array(). I have: ORDER BY payment_methods.ba_type ASC, loan_info.loan_number ASC However, what it does is first orders it by ba_type, then it goes through and re-orders it be loan_number, not keeping the ba_type order intact. I would have much rather handled it all with the SQL, but it just doesn't seem to work. Here's the entire query: SELECT payment_methods.ba_type, loan_info.loan_number, loan_info.id AS loan_id, cust_info.first_name, cust_info.last_name, transactions.approved_date, payment_types.type FROM loan_info, cust_info, transactions, payment_methods, payment_types WHERE payment_methods.id = transactions.payment_method AND payment_types.id = transactions.trans_type AND cust_info.id = loan_info.cust_id AND transactions.loan_id = loan_info.id AND transactions.trans_type = 1 AND (transactions.approved_date = '2003-07-09 00:00:00' AND transactions.approved_date = '2003-07-22 23:59:59') AND (loan_info.loan_number LIKE '101%' OR loan_info.loan_number LIKE '136%' OR loan_info.loan_number LIKE '707%') GROUP BY loan_info.id ORDER BY loan_info.loan_number ASC Maybe I'm doing something wrong there? -Dan Joseph -Original Message- From: Marek Kilimajer [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 22, 2003 11:25 AM To: Dan Joseph Cc: [EMAIL PROTECTED] Subject: Re: [PHP] Array Sorting, 2 items... Your array seems like a result of mysql_fetch_array(), cannot you order it in the sql query? Dan Joseph wrote: Array ( [0] = Array ( [0] = CHECKING [ba_type] = CHECKING [1] = 10132200 [loan_number] = 10132200 Keep everything groupped together in their respective ba_type, and put the numbers in numeric order. I've looked at usort, and many examples of it. I can see how to use it to sort one column, but how would I sort a second to achieve this? Simply if ba_types are equal, continue with checking loan_numbers: function cmp ($a, $b) { if ($a['ba_type'] == $b['ba_type']) { if ($a['loan_number'] == $b['loan_number']) return 0; return ($a['loan_number'] $b['loan_number']) ? -1 : 1; } return ($a['ba_type'] $b['ba_type']) ? -1 : 1; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Array Sorting, 2 items...
-Original Message- From: Dan Joseph [mailto:[EMAIL PROTECTED] Sent: 22 July 2003 16:29 Yeah, I did get it from a mysql_fetch_array(). I have: ORDER BY payment_methods.ba_type ASC, loan_info.loan_number ASC However, what it does is first orders it by ba_type, then it goes through and re-orders it be loan_number, not keeping the ba_type order intact. I would have much rather handled it all with the SQL, but it just doesn't seem to work. Here's the entire query: SELECT payment_methods.ba_type, loan_info.loan_number, loan_info.id AS loan_id, cust_info.first_name, cust_info.last_name, transactions.approved_date, payment_types.type FROM loan_info, cust_info, transactions, payment_methods, payment_types WHERE payment_methods.id = transactions.payment_method AND payment_types.id = transactions.trans_type AND cust_info.id = loan_info.cust_id AND transactions.loan_id = loan_info.id AND transactions.trans_type = 1 AND (transactions.approved_date = '2003-07-09 00:00:00' AND transactions.approved_date = '2003-07-22 23:59:59') AND (loan_info.loan_number LIKE '101%' OR loan_info.loan_number LIKE '136%' OR loan_info.loan_number LIKE '707%') GROUP BY loan_info.id ORDER BY loan_info.loan_number ASC Maybe I'm doing something wrong there? Well, erm, maybe I've got the wrong glasses on today, or maybe you've made a cut'n'paste boo-boo, but it sure looks to me like the ORDER BY clause on your entire query is not the same as the one you posted above. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Array Sorting, 2 items...
Eek... Yes, I did past it wrong... SELECT payment_methods.ba_type, loan_info.loan_number, loan_info.id AS loan_id, cust_info.first_name, cust_info.last_name, transactions.approved_date, payment_types.type FROM loan_info, cust_info, transactions, payment_methods, payment_types WHERE payment_methods.id = transactions.payment_method AND payment_types.id = transactions.trans_type AND cust_info.id = loan_info.cust_id AND transactions.loan_id = loan_info.id AND transactions.trans_type = 1 AND (transactions.approved_date = '2003-07-09 00:00:00' AND transactions.approved_date = '2003-07-22 23:59:59') AND (loan_info.loan_number LIKE '101%' OR loan_info.loan_number LIKE '136%' OR loan_info.loan_number LIKE '707%') GROUP BY loan_info.id ORDER BY payment_methods.ba_type ASC, loan_info.loan_number ASC That's the right query. Anything wrong with that? Shouldn't that give me the sort I'm looking for without having to do a usort? -Dan Joseph -Original Message- From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 22, 2003 11:39 AM To: 'Dan Joseph'; [EMAIL PROTECTED] Subject: RE: [PHP] Array Sorting, 2 items... -Original Message- From: Dan Joseph [mailto:[EMAIL PROTECTED] Sent: 22 July 2003 16:29 Yeah, I did get it from a mysql_fetch_array(). I have: ORDER BY payment_methods.ba_type ASC, loan_info.loan_number ASC However, what it does is first orders it by ba_type, then it goes through and re-orders it be loan_number, not keeping the ba_type order intact. I would have much rather handled it all with the SQL, but it just doesn't seem to work. Here's the entire query: SELECT payment_methods.ba_type, loan_info.loan_number, loan_info.id AS loan_id, cust_info.first_name, cust_info.last_name, transactions.approved_date, payment_types.type FROM loan_info, cust_info, transactions, payment_methods, payment_types WHERE payment_methods.id = transactions.payment_method AND payment_types.id = transactions.trans_type AND cust_info.id = loan_info.cust_id AND transactions.loan_id = loan_info.id AND transactions.trans_type = 1 AND (transactions.approved_date = '2003-07-09 00:00:00' AND transactions.approved_date = '2003-07-22 23:59:59') AND (loan_info.loan_number LIKE '101%' OR loan_info.loan_number LIKE '136%' OR loan_info.loan_number LIKE '707%') GROUP BY loan_info.id ORDER BY loan_info.loan_number ASC Maybe I'm doing something wrong there? Well, erm, maybe I've got the wrong glasses on today, or maybe you've made a cut'n'paste boo-boo, but it sure looks to me like the ORDER BY clause on your entire query is not the same as the one you posted above. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting, 2 items...
* Thus wrote Dan Joseph ([EMAIL PROTECTED]): Eek... Yes, I did past it wrong... SELECT payment_methods.ba_type, loan_info.loan_number, loan_info.id AS loan_id, cust_info.first_name, cust_info.last_name, transactions.approved_date, payment_types.type FROM loan_info, cust_info, transactions, payment_methods, payment_types WHERE payment_methods.id = transactions.payment_method AND payment_types.id = transactions.trans_type AND cust_info.id = loan_info.cust_id AND transactions.loan_id = loan_info.id AND transactions.trans_type = 1 AND (transactions.approved_date = '2003-07-09 00:00:00' AND transactions.approved_date = '2003-07-22 23:59:59') AND (loan_info.loan_number LIKE '101%' OR loan_info.loan_number LIKE '136%' OR loan_info.loan_number LIKE '707%') GROUP BY loan_info.id ORDER BY payment_methods.ba_type ASC, loan_info.loan_number ASC That's the right query. Anything wrong with that? Shouldn't that give me the sort I'm looking for without having to do a usort? Correct, that should sort properly. If that isn't workin I would get on the database servers mailing list (if they have one). Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Array Sorting, 2 items...
Uhhh, I could have sworn I posted the right one, here it is again... SELECT payment_methods.ba_type, loan_info.loan_number, loan_info.id AS loan_id, cust_info.first_name, cust_info.last_name, transactions.approved_date, payment_types.type FROM loan_info, cust_info, transactions, payment_methods, payment_types WHERE payment_methods.id = transactions.payment_method AND payment_types.id = transactions.trans_type AND cust_info.id = loan_info.cust_id AND transactions.loan_id = loan_info.id AND transactions.trans_type = 1 AND (transactions.approved_date = '2003-07-09 00:00:00' AND transactions.approved_date = '2003-07-22 23:59:59') AND (loan_info.loan_number LIKE '101%' OR loan_info.loan_number LIKE '136%' OR loan_info.loan_number LIKE '707%') GROUP BY loan_info.id ORDER BY payment_methods.ba_type ASC, loan_info.loan_number ASC Any ideas? -Dan Joseph -Original Message- From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 22, 2003 11:39 AM To: 'Dan Joseph'; [EMAIL PROTECTED] Subject: RE: [PHP] Array Sorting, 2 items... -Original Message- From: Dan Joseph [mailto:[EMAIL PROTECTED] Sent: 22 July 2003 16:29 Yeah, I did get it from a mysql_fetch_array(). I have: ORDER BY payment_methods.ba_type ASC, loan_info.loan_number ASC However, what it does is first orders it by ba_type, then it goes through and re-orders it be loan_number, not keeping the ba_type order intact. I would have much rather handled it all with the SQL, but it just doesn't seem to work. Here's the entire query: SELECT payment_methods.ba_type, loan_info.loan_number, loan_info.id AS loan_id, cust_info.first_name, cust_info.last_name, transactions.approved_date, payment_types.type FROM loan_info, cust_info, transactions, payment_methods, payment_types WHERE payment_methods.id = transactions.payment_method AND payment_types.id = transactions.trans_type AND cust_info.id = loan_info.cust_id AND transactions.loan_id = loan_info.id AND transactions.trans_type = 1 AND (transactions.approved_date = '2003-07-09 00:00:00' AND transactions.approved_date = '2003-07-22 23:59:59') AND (loan_info.loan_number LIKE '101%' OR loan_info.loan_number LIKE '136%' OR loan_info.loan_number LIKE '707%') GROUP BY loan_info.id ORDER BY loan_info.loan_number ASC Maybe I'm doing something wrong there? Well, erm, maybe I've got the wrong glasses on today, or maybe you've made a cut'n'paste boo-boo, but it sure looks to me like the ORDER BY clause on your entire query is not the same as the one you posted above. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Array Sorting, 2 items...
Hi, You know, this worked just fine, as did my order by. Its my brain that is completely wacked. I am going about this all wrong Thanks for all your help.. Take care.. I'll let you know how it goes. -Dan Joseph -Original Message- From: Marek Kilimajer [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 22, 2003 11:25 AM To: Dan Joseph Cc: [EMAIL PROTECTED] Subject: Re: [PHP] Array Sorting, 2 items... Your array seems like a result of mysql_fetch_array(), cannot you order it in the sql query? Dan Joseph wrote: Array ( [0] = Array ( [0] = CHECKING [ba_type] = CHECKING [1] = 10132200 [loan_number] = 10132200 Keep everything groupped together in their respective ba_type, and put the numbers in numeric order. I've looked at usort, and many examples of it. I can see how to use it to sort one column, but how would I sort a second to achieve this? Simply if ba_types are equal, continue with checking loan_numbers: function cmp ($a, $b) { if ($a['ba_type'] == $b['ba_type']) { if ($a['loan_number'] == $b['loan_number']) return 0; return ($a['loan_number'] $b['loan_number']) ? -1 : 1; } return ($a['ba_type'] $b['ba_type']) ? -1 : 1; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Array Sorting
I have an array that looks like this: $i = 0; $item[$i] = array( 'link' = 'http://...', 'image' = '/images/image.jpg', 'title' = 'some title', 'price' = '$14.00', 'cat'= 'Frames', 'author' = 'Pinochio', 'artist' = '', 'asin' = '010101', 'manufacturer' = 'Post' ); $i++; $item[$i] = array( 'link' = 'http://...', 'image' = '/images/something.jpg', 'title' = 'this is fun', 'price' = '$2.99', 'cat'= 'Card', 'author' = 'Mickey', 'artist' = '', 'asin' = '1116221', 'manufacturer' = 'Kraft' ); $i++; etc., etc. I would like to sort $items based on the manufacturer of each array within. So that, in the above example, the second one would come before the first. Is there a way to do that? -- H| I haven't lost my mind; it's backed up on tape somewhere. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / WebSmith . 800.441.3873 x130 Photo Craft Laboratories, Inc.. 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting
Use usort. This function should do the work: function cmp ($a, $b) { if ($a['manufacturer'] == $b['manufacturer']) return 0; return ($a['manufacturer'] $b['manufacturer']) ? -1 : 1; } Ashley M. Kirchner wrote: I have an array that looks like this: $i = 0; $item[$i] = array( 'link' = 'http://...', 'image' = '/images/image.jpg', 'title' = 'some title', 'price' = '$14.00', 'cat'= 'Frames', 'author' = 'Pinochio', 'artist' = '', 'asin' = '010101', 'manufacturer' = 'Post' ); $i++; $item[$i] = array( 'link' = 'http://...', 'image' = '/images/something.jpg', 'title' = 'this is fun', 'price' = '$2.99', 'cat'= 'Card', 'author' = 'Mickey', 'artist' = '', 'asin' = '1116221', 'manufacturer' = 'Kraft' ); $i++; etc., etc. I would like to sort $items based on the manufacturer of each array within. So that, in the above example, the second one would come before the first. Is there a way to do that? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting
Marek Kilimajer wrote: Use usort. This function should do the work: Hey thanks! That worked like a charm, once I figured out that making the comparison instead of I would get an ascending sort instead of the descending one. Now, can I do multiple sorts? Like, sort on cat first, manufacturer next, and title last? Here's the array structure again: $i = 0; $item[$i] = array( 'link' = 'http://...', 'image'= '/images/image.jpg', 'title'= 'some title', 'price'= '$14.00', 'cat' = 'Frames', 'author' = 'Pinochio', 'artist' = '', 'asin' = '010101', 'manufacturer' = 'Post' ); $i++; $item[$i] = array( 'link' = 'http://...', 'image'= '/images/something.jpg', 'title'= 'this is fun', 'price'= '$2.99', 'cat' = 'Card', 'author' = 'Mickey', 'artist' = '', 'asin' = '1116221', 'manufacturer' = 'Kraft' ); $i++; ...etc., etc. -- H| I haven't lost my mind; it's backed up on tape somewhere. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / WebSmith . 800.441.3873 x130 Photo Craft Laboratories, Inc.. 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting
Yes, simply don't return 0 when the manufacturers are equal, but continue with other checks: function cmp ($a, $b) { if ($a['cat'] == $b['cat']) { // return 0; -- not anymore, but go on to compare manuf. if ($a['manufacturer'] == $b['manufacturer']) { if ($a['title'] == $b['title']) { // even title is the same, return 0 return 0; } return ($a['title'] $b['title']) ? -1 : 1; } return ($a['manufacturer'] $b['manufacturer']) ? -1 : 1; } return ($a['cat'] $b['cat']) ? -1 : 1; } Ashley M. Kirchner wrote: Marek Kilimajer wrote: Use usort. This function should do the work: Hey thanks! That worked like a charm, once I figured out that making the comparison instead of I would get an ascending sort instead of the descending one. Now, can I do multiple sorts? Like, sort on cat first, manufacturer next, and title last? Here's the array structure again: $i = 0; $item[$i] = array( 'link' = 'http://...', 'image'= '/images/image.jpg', 'title'= 'some title', 'price'= '$14.00', 'cat' = 'Frames', 'author' = 'Pinochio', 'artist' = '', 'asin' = '010101', 'manufacturer' = 'Post' ); $i++; $item[$i] = array( 'link' = 'http://...', 'image'= '/images/something.jpg', 'title'= 'this is fun', 'price'= '$2.99', 'cat' = 'Card', 'author' = 'Mickey', 'artist' = '', 'asin' = '1116221', 'manufacturer' = 'Kraft' ); $i++; ...etc., etc. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting
BTW, this is what I always receive when mailing you: Final-Recipient: RFC822; [EMAIL PROTECTED] Action: failed Status: 5.7.1 Remote-MTA: DNS; serpico.pcraft.com Diagnostic-Code: SMTP; 554 5.7.1 Original message rejected due to high SPAM score: 97.4 Last-Attempt-Date: Thu, 19 Jun 2003 11:36:23 +0200 (CEST) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array Sorting
Marek Kilimajer wrote: BTW, this is what I always receive when mailing you: Diagnostic-Code: SMTP; 554 5.7.1 Original message rejected due to high SPAM score: 97.4 Hrm, yes. Has to do with your .sk domain I'm sure. Bloody automatic filters. Thanks for the function, it works as expected. Now let me go kick those filters... -- H| I haven't lost my mind; it's backed up on tape somewhere. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / WebSmith . 800.441.3873 x130 Photo Craft Laboratories, Inc.. 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Array sorting
My question is if anyone have any idé on how to sort this array so that the names and ages are sorted after Close Friend, Friend, Wife and last Family... $A[] = array (Nils, 23, Friend); $A[] = array (Emma, 21, Friend); $A[] = array (Cassie, 21, Wife); $A[] = array (Zydox, 23, Friend); $A[] = array (Patrick, 24, Close Friend); $A[] = array (Kalle, 40, Family); $A[] = array (John, 10, Close Friend); $A[] = array (Anna, 12, Friend); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Array Sorting
I have looked through php.net and the books that I have and I am confused, so I hope that someone can help me out here. I am trying to sort an array that I have. I use a while statement to fill the array: $teams[++$i][team_id]=$row-team_id; $teams[$i][name]=$row1-name; $teams[$i][team_sea_id]=$row-team_sea_id; Everything goes in the array as it should, so no problem there. I just want to know how do I sort the array by the name column? Thanks In Advance Steve -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php