Re: [PHP] Sorting an array

2011-03-01 Thread Jim Lucas
On 2/28/2011 7:52 PM, Ron Piggott wrote:
 
 I need help to know how to sort the words / phrases in my array.
 
 Variable name: $words_used
 print_r( $words_used ); Current output: Array ( [187] = Sin [249] = 
 Punished [98] = Sanctuary [596] = Sing [362] = Anointing Oil )
 Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = 
 Punished [98] = Sanctuary [187] = Sin [596] = Sing ) 
 
 The #’s are the auto_increment value of the word in the mySQL database.  The 
 number is not representative of alphabetical order, but the order it was 
 added to the database.
 
 Thank you for your assistance.
 
 Ron
 
 The Verse of the Day
 “Encouragement from God’s Word”
 http://www.TheVerseOfTheDay.info  
 

Besides the answer others have pointed you to, I would recommend doing it in 
mysql?

SELECT * FROM table ORDER BY your_column;

Check out the syntax for it here.

http://dev.mysql.com/doc/refman/5.0/en/select.html
and
http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html

Jim Lucas

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Sorting an array

2011-02-28 Thread Ron Piggott

I need help to know how to sort the words / phrases in my array.

Variable name: $words_used
print_r( $words_used ); Current output: Array ( [187] = Sin [249] = Punished 
[98] = Sanctuary [596] = Sing [362] = Anointing Oil )
Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = 
Punished [98] = Sanctuary [187] = Sin [596] = Sing ) 

The #’s are the auto_increment value of the word in the mySQL database.  The 
number is not representative of alphabetical order, but the order it was added 
to the database.

Thank you for your assistance.

Ron

The Verse of the Day
“Encouragement from God’s Word”
http://www.TheVerseOfTheDay.info  


Re: [PHP] Sorting an array

2011-02-28 Thread Simon J Welsh
On 1/03/2011, at 4:52 PM, Ron Piggott wrote:

 
 I need help to know how to sort the words / phrases in my array.
 
 Variable name: $words_used
 print_r( $words_used ); Current output: Array ( [187] = Sin [249] = 
 Punished [98] = Sanctuary [596] = Sing [362] = Anointing Oil )
 Desired result: Alphabetical sort: Array ( [362] = Anointing Oil [249] = 
 Punished [98] = Sanctuary [187] = Sin [596] = Sing ) 
 
 The #’s are the auto_increment value of the word in the mySQL database.  The 
 number is not representative of alphabetical order, but the order it was 
 added to the database.
 
 Thank you for your assistance.
 
 Ron
 
 The Verse of the Day
 “Encouragement from God’s Word”
 http://www.TheVerseOfTheDay.info  

Have a look at asort() (http://php.net/asort), which is used to sort arrays but 
maintain the key association. The default search option should work fine for 
you, if not try one of the other SORT_* flags described on the sort() manual 
page.
---
Simon Welsh
Admin of http://simon.geek.nz/

Who said Microsoft never created a bug-free program? The blue screen never, 
ever crashes!

http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Sorting an array of sub-arrays based on a sub-array's key

2009-09-06 Thread James Colannino
Hey everyone.  I have an array that looks like this:

$main_array[0] = array('key1' = 'vala');
$main_array[1] = array('key1' = 'valb');
etc.

I want to sort the main array based on the value of key1 for each
sub-array.  I looked at all the array sorting functions, but unless I
misunderstood something, I didn't see a direct way to do what I want.

If there were a sorting function in which I could pass as an argument
the name of a function that compares two elements like qsort in C, I
could do it easily.  Is there a function like that in PHP?  If not, what
should I do?

Thanks everyone!
James

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sorting an array of sub-arrays based on a sub-array's key

2009-09-06 Thread Eddie Drapkin
On Sun, Sep 6, 2009 at 6:45 PM, James Colanninoja...@colannino.org wrote:
 Hey everyone.  I have an array that looks like this:

 $main_array[0] = array('key1' = 'vala');
 $main_array[1] = array('key1' = 'valb');
 etc.

 I want to sort the main array based on the value of key1 for each
 sub-array.  I looked at all the array sorting functions, but unless I
 misunderstood something, I didn't see a direct way to do what I want.

 If there were a sorting function in which I could pass as an argument
 the name of a function that compares two elements like qsort in C, I
 could do it easily.  Is there a function like that in PHP?  If not, what
 should I do?

 Thanks everyone!
 James

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



http://us3.php.net/uasort

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sorting an array of sub-arrays based on a sub-array's key

2009-09-06 Thread James Colannino
Eddie Drapkin wrote:

 http://us3.php.net/uasort

Exactly what I was looking for.  Thanks.

James

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP sorting csv array output

2007-05-15 Thread Anna Vester

Yep, that would be the perfect solution, but, unfortunately, database
is not an option for this project. Thanks for looking! I did get a
solution though from another list.
Here is a working version:

http://veanndesign.com/sorting.php

compare it to the not working one:

http://veanndesign.com/test.php

Anyways, thanks again!

Anna

On 5/13/07, Richard Lynch [EMAIL PROTECTED] wrote:

If you are going to sort it by various fields, I'd just throw it into
a database...

That said, http://php.net/usort should be able to do whatever you want.

On Thu, May 10, 2007 2:18 pm, Anna Vester wrote:
 Hello all,

 I have a question concerning .CSV array sorting. I have tried googling
 for an answer and have tried different techniques, but nothing seems
 to works as I would like it to work. Here is my situation:
 Test file is located here: http://veanndesign.com/test.php

 I would like to be able to sort the output by the Time Zone (or any
 other fields). Here is how my code looks like:
 http://veanndesign.com/test.html

 I believe that I need to get all the data from the .csv file dumped
 into 1 array, and I guess I am struggling at that point. I have tried
 using foreach inside of the while loop, but it doesn't seem to work.
 So what is the best and/or right way to sort this type of data?

 Hopefully, this email makes sense.

 Thanks in advance.

 --
 Anna Vester
 Web Designer
 http://www.veanndesign.com

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




--
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?





--
Anna Vester
Web Designer
http://www.veanndesign.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP sorting csv array output

2007-05-13 Thread Richard Lynch
If you are going to sort it by various fields, I'd just throw it into
a database...

That said, http://php.net/usort should be able to do whatever you want.

On Thu, May 10, 2007 2:18 pm, Anna Vester wrote:
 Hello all,

 I have a question concerning .CSV array sorting. I have tried googling
 for an answer and have tried different techniques, but nothing seems
 to works as I would like it to work. Here is my situation:
 Test file is located here: http://veanndesign.com/test.php

 I would like to be able to sort the output by the Time Zone (or any
 other fields). Here is how my code looks like:
 http://veanndesign.com/test.html

 I believe that I need to get all the data from the .csv file dumped
 into 1 array, and I guess I am struggling at that point. I have tried
 using foreach inside of the while loop, but it doesn't seem to work.
 So what is the best and/or right way to sort this type of data?

 Hopefully, this email makes sense.

 Thanks in advance.

 --
 Anna Vester
 Web Designer
 http://www.veanndesign.com

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] PHP sorting csv array output

2007-05-10 Thread Anna Vester

Hello all,

I have a question concerning .CSV array sorting. I have tried googling
for an answer and have tried different techniques, but nothing seems
to works as I would like it to work. Here is my situation:
Test file is located here: http://veanndesign.com/test.php

I would like to be able to sort the output by the Time Zone (or any
other fields). Here is how my code looks like:
http://veanndesign.com/test.html

I believe that I need to get all the data from the .csv file dumped
into 1 array, and I guess I am struggling at that point. I have tried
using foreach inside of the while loop, but it doesn't seem to work.
So what is the best and/or right way to sort this type of data?

Hopefully, this email makes sense.

Thanks in advance.

--
Anna Vester
Web Designer
http://www.veanndesign.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP sorting csv array output

2007-05-10 Thread Daniel Brown

   One place to start reading, Anna, would be the PHP manual for the
fgetcsv() function, which is specifically for CSV parsing.

   http://www.php.net/fgetcsv

On 5/10/07, Anna Vester [EMAIL PROTECTED] wrote:


Hello all,

I have a question concerning .CSV array sorting. I have tried googling
for an answer and have tried different techniques, but nothing seems
to works as I would like it to work. Here is my situation:
Test file is located here: http://veanndesign.com/test.php

I would like to be able to sort the output by the Time Zone (or any
other fields). Here is how my code looks like:
http://veanndesign.com/test.html

I believe that I need to get all the data from the .csv file dumped
into 1 array, and I guess I am struggling at that point. I have tried
using foreach inside of the while loop, but it doesn't seem to work.
So what is the best and/or right way to sort this type of data?

Hopefully, this email makes sense.

Thanks in advance.

--
Anna Vester
Web Designer
http://www.veanndesign.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107


Re: [PHP] PHP sorting csv array output

2007-05-10 Thread Anna Vester

On 5/10/07, Daniel Brown [EMAIL PROTECTED] wrote:


One place to start reading, Anna, would be the PHP manual for the
fgetcsv() function, which is specifically for CSV parsing.

http://www.php.net/fgetcsv


Thanks for your quick reply Daniel. Yes I've seen that function before
and i am using it to parse the file, which displays fine. I just need
to be able to sort that data (by Time Zone) prior to displaying. Sorry
If I wasn't clear.

Thanks.


--
Anna Vester
Web Designer
http://www.veanndesign.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP sorting csv array output

2007-05-10 Thread tg-php
When you load your data into your array, you can use the timezone (or whatever 
field you're using to sort) as the array key, then use ksort().

Check the see also for a bunch of other types of sorting you can do.

When it comes to the multisorts and user defined sorts, I'm at a bit of a 
loss..hah.. just never had to use them so they're still a bit mystical to me.

-TG

http://us2.php.net/manual/en/function.ksort.php

= = = Original message = = =

On 5/10/07, Daniel Brown [EMAIL PROTECTED] wrote:

 One place to start reading, Anna, would be the PHP manual for the
 fgetcsv() function, which is specifically for CSV parsing.

 http://www.php.net/fgetcsv

Thanks for your quick reply Daniel. Yes I've seen that function before
and i am using it to parse the file, which displays fine. I just need
to be able to sort that data (by Time Zone) prior to displaying. Sorry
If I wasn't clear.

Thanks.


-- 
Anna Vester
Web Designer
http://www.veanndesign.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sorting multi array

2007-04-25 Thread Jon Bennett

hi,

I have the following array, which I need to sort by quantity...

Array
(
   [2408] = Array
   (
   [name] = Havaianas Top Pink Crystal
   [size] = 5 (37/38)
   [quantity] = 4
   )

   [3388] = Array
   (
   [name] = Havaianas Brazil Silver
   [size] = 6/7 (39/40)
   [quantity] = 6
   )

   [2666] = Array
   (
   [name] = Havaianas Brasil Black
   [size] = 8/9 (41/42)
   [quantity] = 1
   )

   [3210] = Array
   (
   [name] = Havaianas Margaridas Yellow
   [size] = 5 (37/38)
   [quantity] = 1
   )

   [2552] = Array
   (
   [name] = Havaianas Flash White
   [size] = 5 (37/38)
   [quantity] = 1
   )
)

I need to keep the indexes if poss.

Many thanks,

jon

--


jon bennett
t: +44 (0) 1225 341 039 w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting multi array

2007-04-25 Thread Frank Arensmeier
Jon, I would suggest that you should have a look at the function  
array_multisort. See the manual for details on what this function  
is capable of.


//frank
25 apr 2007 kl. 01.58 skrev Jon Bennett:


hi,

I have the following array, which I need to sort by quantity...

Array
(
   [2408] = Array
   (
   [name] = Havaianas Top Pink Crystal
   [size] = 5 (37/38)
   [quantity] = 4
   )

   [3388] = Array
   (
   [name] = Havaianas Brazil Silver
   [size] = 6/7 (39/40)
   [quantity] = 6
   )

   [2666] = Array
   (
   [name] = Havaianas Brasil Black
   [size] = 8/9 (41/42)
   [quantity] = 1
   )

   [3210] = Array
   (
   [name] = Havaianas Margaridas Yellow
   [size] = 5 (37/38)
   [quantity] = 1
   )

   [2552] = Array
   (
   [name] = Havaianas Flash White
   [size] = 5 (37/38)
   [quantity] = 1
   )
)

I need to keep the indexes if poss.

Many thanks,

jon

--


jon bennett
t: +44 (0) 1225 341 039 w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--
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] sorting multi array

2007-04-25 Thread Chris Boget
 I have the following array, which I need to sort by quantity...
 I need to keep the indexes if poss.

This may not be the most elegant solution.  Let's call your array
$origArray

$tmpArray = array();
foreach( $origArray as $elKey = $elArray ) {
  $tmpArray[$elArray['quantity']] = $elKey;

}

if( ksort( $tmpArray )) {
  $sortedArray = array();
  foreach( $tmpArray as $elKey ) {
$sortedArray[$elKey] = $origArray[$elKey];

  }
  echo 'Original: pre' . print_r( $origArray, TRUE ) '/prebr';
  echo 'Sorted: pre' . print_r( $sortedArray, TRUE ) '/pre';

} else {
  echo 'Sorry, couldn\'t sort';

}

thnx,
Chris

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] sorting multi array

2007-04-25 Thread Zoltán Németh
2007. 04. 25, szerda keltezéssel 11.39-kor Chris Boget ezt írta:
  I have the following array, which I need to sort by quantity...
  I need to keep the indexes if poss.
 
 This may not be the most elegant solution.  Let's call your array
 $origArray
 
 $tmpArray = array();
 foreach( $origArray as $elKey = $elArray ) {
   $tmpArray[$elArray['quantity']] = $elKey;
 
 }
 
 if( ksort( $tmpArray )) {
   $sortedArray = array();
   foreach( $tmpArray as $elKey ) {
 $sortedArray[$elKey] = $origArray[$elKey];
 
   }
   echo 'Original: pre' . print_r( $origArray, TRUE ) '/prebr';
   echo 'Sorted: pre' . print_r( $sortedArray, TRUE ) '/pre';
 
 } else {
   echo 'Sorry, couldn\'t sort';
 
 }

this won't work if he has the same quantity for several keys, I think

greets
Zoltán Németh

 
 thnx,
 Chris
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting multi array

2007-04-25 Thread Chris Boget

this won't work if he has the same quantity for several keys, I think


Yes, you are correct.  If that is the case, then you would just need to 
change the following line


$tmpArray[$elArray['quantity']] = $elKey;

to

$tmpArray[$elArray['quantity']][] = $elKey;

then change logic in this loop:

foreach( $tmpArray as $elKey ) {

to handle the additional array.  If additional sorting needs to occur for 
those keys that have the same quantity then I think recursion will be in 
order.


thnx,
Chris

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting multi array

2007-04-25 Thread [EMAIL PROTECTED]
array_multisort accepts column arrays but here you try to sort row based  
arrays.


try this:
?
class ArrayUtility
{
/* Sorts an array by a member */
static private $sortMember;
static function sortByMember($array, $member)
{
self::$sortMember = $member;
usort($array, array('ArrayUtility', 'compareMember'));
}

static function compareMember($a, $b)
{
return $a[self::$sortMember]  $b[self::$sortMember];
}
}


ArrayUtility::sortByMember($array, 'quantity');

?
Am 25.04.2007, 12:36 Uhr, schrieb Frank Arensmeier  
[EMAIL PROTECTED]:


Jon, I would suggest that you should have a look at the function  
array_multisort. See the manual for details on what this function is  
capable of.


//frank
25 apr 2007 kl. 01.58 skrev Jon Bennett:


hi,

I have the following array, which I need to sort by quantity...

Array
(
   [2408] = Array
   (
   [name] = Havaianas Top Pink Crystal
   [size] = 5 (37/38)
   [quantity] = 4
   )

   [3388] = Array
   (
   [name] = Havaianas Brazil Silver
   [size] = 6/7 (39/40)
   [quantity] = 6
   )

   [2666] = Array
   (
   [name] = Havaianas Brasil Black
   [size] = 8/9 (41/42)
   [quantity] = 1
   )

   [3210] = Array
   (
   [name] = Havaianas Margaridas Yellow
   [size] = 5 (37/38)
   [quantity] = 1
   )

   [2552] = Array
   (
   [name] = Havaianas Flash White
   [size] = 5 (37/38)
   [quantity] = 1
   )
)

I need to keep the indexes if poss.

Many thanks,

jon

--

jon bennett
t: +44 (0) 1225 341 039 w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--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] sorting multi array

2007-04-25 Thread Myron Turner

Jon Bennett wrote:

hi,

I have the following array, which I need to sort by quantity...

Array
(
   [2408] = Array
   (
   [name] = Havaianas Top Pink Crystal
   [size] = 5 (37/38)
   [quantity] = 4
   )

   [3388] = Array
   (
   [name] = Havaianas Brazil Silver
   [size] = 6/7 (39/40)
   [quantity] = 6
   )

   [2666] = Array
   (
   [name] = Havaianas Brasil Black
   [size] = 8/9 (41/42)
   [quantity] = 1
   )

   [3210] = Array
   (
   [name] = Havaianas Margaridas Yellow
   [size] = 5 (37/38)
   [quantity] = 1
   )

   [2552] = Array
   (
   [name] = Havaianas Flash White
   [size] = 5 (37/38)
   [quantity] = 1
   )
)

I need to keep the indexes if poss.

Many thanks,

jon


This works for me:


function cmp($a, $b)
{
   $aq = $a['quantity'];
   $bq = $b['quantity'];

   if ($a == $b) {
   return 0;
   }
   return ($aq  $bq) ? -1 : 1;
}

uasort($data, cmp);

To reverse the sort order:  return ($aq  $bq) ? -1 : 1;

--

_
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting multi array

2007-04-25 Thread Richard Lynch
Search the archives for multisort array and you should find this
thread a few thousand times...

On Tue, April 24, 2007 6:58 pm, Jon Bennett wrote:
 hi,

 I have the following array, which I need to sort by quantity...

 Array
 (
 [2408] = Array
 (
 [name] = Havaianas Top Pink Crystal
 [size] = 5 (37/38)
 [quantity] = 4
 )

 [3388] = Array
 (
 [name] = Havaianas Brazil Silver
 [size] = 6/7 (39/40)
 [quantity] = 6
 )

 [2666] = Array
 (
 [name] = Havaianas Brasil Black
 [size] = 8/9 (41/42)
 [quantity] = 1
 )

 [3210] = Array
 (
 [name] = Havaianas Margaridas Yellow
 [size] = 5 (37/38)
 [quantity] = 1
 )

 [2552] = Array
 (
 [name] = Havaianas Flash White
 [size] = 5 (37/38)
 [quantity] = 1
 )
 )

 I need to keep the indexes if poss.

 Many thanks,

 jon

 --


 jon bennett
 t: +44 (0) 1225 341 039 w: http://www.jben.net/
 iChat (AIM): jbendotnet Skype: jon-bennett

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sorting Multidimensional Array

2006-10-31 Thread Richard Lynch
On Tue, October 31, 2006 11:10 am, Keith Spiller wrote:
 RE:  Sorting Multidimensional Array

 I'm trying to sort a multidimensional array.  The data was taken from
 a mysql query:

 $myrow = mysql_fetch_row($result) {
   query[] = $myrow;
 }

 The purpose is to retrieve the table data and manually add a record,
 then sort ASC by the startdate which is the forth field...

 Something like:

 $test = array_multisort($query, $key = '$query[4]');

 Any help would be greatly appreciated.  Thanks,

Don't do that. :-)

For starters, sorting in PHP is MUCH less efficient than in a DB.

Secondly, getting multi-dimensional (sic) arrays in PHP to sort like
you want generates a ton of traffic here, so it must be hard. :-)

You can do somethig like this:

(SELECT x, y, z FROM real_data
 UNION
 SELECT 'manually', 'inserted', 'data' as z
)
ORDER BY z

And achieve MUCH better results with far less headache.

If 'z' is indexed in real_data, add another ORDER BY z right before
the UNION -- That will probably make it very fast/easy for the DB to
splice in your manually-inserted 'z' value.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sorting Multidimensional Array

2006-10-31 Thread Keith Spiller

Thanks Richard,

But wouldn't order by z interfere with my original order by values?

My code is as follows:

  $fields1  =  ID, Title, Label, Location, Start, End, Time, ;
  $fields1 .=  Description, Organization, Department, Contact, ;
  $fields1 .=  Phone, Email, Global, Board, Committee, Status, TBD_Time ;

  $command  =  SELECT $fields1, TO_DAYS(End) - TO_DAYS(Start) + 1 AS Days 
;

  $command .=  FROM $tablename ;
  $command .=  $WHERETXT ORDER BY $sortby $sortorder ;


I just need to add a new record, but I have to use PHP because the Start and 
End

dates for that record must match the current month...

If I could add the record to the Select statement as you suggest that would 
be wonderful.



Keith


- Original Message - 
From: Richard Lynch [EMAIL PROTECTED]

To: Keith Spiller [EMAIL PROTECTED]
Cc: php-general@lists.php.net
Sent: Tuesday, October 31, 2006 10:48 AM
Subject: Re: [PHP] Sorting Multidimensional Array



On Tue, October 31, 2006 11:10 am, Keith Spiller wrote:

RE:  Sorting Multidimensional Array

I'm trying to sort a multidimensional array.  The data was taken from
a mysql query:

$myrow = mysql_fetch_row($result) {
  query[] = $myrow;
}

The purpose is to retrieve the table data and manually add a record,
then sort ASC by the startdate which is the forth field...

Something like:

$test = array_multisort($query, $key = '$query[4]');

Any help would be greatly appreciated.  Thanks,


Don't do that. :-)

For starters, sorting in PHP is MUCH less efficient than in a DB.

Secondly, getting multi-dimensional (sic) arrays in PHP to sort like
you want generates a ton of traffic here, so it must be hard. :-)

You can do somethig like this:

(SELECT x, y, z FROM real_data
UNION
SELECT 'manually', 'inserted', 'data' as z
)
ORDER BY z

And achieve MUCH better results with far less headache.

If 'z' is indexed in real_data, add another ORDER BY z right before
the UNION -- That will probably make it very fast/easy for the DB to
splice in your manually-inserted 'z' value.

--
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So? 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Sorting Multidimensional Array

2006-10-31 Thread Keith Spiller

Hi,

RE:  Sorting Multidimensional Array

I'm trying to sort a multidimensional array.  The data was taken from
a mysql query:

$myrow = mysql_fetch_row($result) {
 query[] = $myrow;
}

The purpose is to retrieve the table data and manually add a record,
then sort ASC by the startdate which is the forth field...

Something like:
 
$test = array_multisort($query, $key = '$query[4]');


Any help would be greatly appreciated.  Thanks,


Keith

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting in array

2006-08-03 Thread Richard Lynch
For starters, the function is supposed to return 0 for countries that
are equal.

As it stands now, it's going to return 1 / -1 randomly based on which
arg happens to be $a versus $b.

That's bad.

For some implementations of shuffle/sort routines, it will actually
crash.

I forget which implementation does this, but it's got to do with
caching the comparison result in a B-tree and then walking the tree
assuming that the comparison will be consistent...

Anyway, as far as doesn't work goes, you'd have to give us more info
about how it's not working...

On Mon, July 31, 2006 1:56 am, weetat wrote:

 Hi ,

Doesn't work .
Any ideas ?

 Thanks
 Peter Lauri wrote:
function cmpcountry($a, $b)
{

  $country1 = $a['country'];
  $country2 = $b['country'];

 if($country1=='') return 1;
 else return ($country1  $country2) ? -1 : 1;

}

 -Original Message-
 From: weetat [mailto:[EMAIL PROTECTED]
 Sent: Monday, July 31, 2006 12:32 PM
 To: php-general@lists.php.net
 Subject: [PHP] sorting in array

 Hi all ,

   I have array value as shown below, i have paste my test php code
 below:
   I have problem when doing usort() when 'country' = '', i would
 like to
 display records where country = '' last. Any ideas how to do that ?

 Thanks

$arraytest= array(
   array
  (
  'country' = '',
  )
   ,
   array
  (
  'country' = 'Thailand',
  )
   ,
   array
  (
  'country' = 'Singapore',
  )
   ,
   array
  (
  'country' = 'Singapore',
  )
   ,
   array
  (
  'country' = '',
  )
,
array
  (
  'country' = '',
  )
  ,
  array
  (
  'country' = '',
  )

   );


function cmpcountry($a, $b)
{

  $country1 = $a['country'];
  $country2 = $b['country'];

   return ($country1  $country2) ? -1 : 1;
}

  usort($arraytest,cmpcountry);
  while(list($name,$value)=each($arraytest)){
 echo $name.brbr;

 while(list($n,$v) = each($arraytest[$name])){
 echo $v.brbr;
 }
   }


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
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] sorting in array

2006-07-31 Thread weetat


Hi ,

  Doesn't work .
  Any ideas ?

Thanks
Peter Lauri wrote:

   function cmpcountry($a, $b)
   {

 $country1 = $a['country'];
 $country2 = $b['country'];

   if($country1=='') return 1;
   else return ($country1  $country2) ? -1 : 1;

   }

-Original Message-
From: weetat [mailto:[EMAIL PROTECTED] 
Sent: Monday, July 31, 2006 12:32 PM

To: php-general@lists.php.net
Subject: [PHP] sorting in array

Hi all ,

  I have array value as shown below, i have paste my test php code below:
  I have problem when doing usort() when 'country' = '', i would like to 
display records where country = '' last. Any ideas how to do that ?


Thanks

   $arraytest= array(
  array
 (
 'country' = '',
 )
  ,
  array
 (
 'country' = 'Thailand',
 )
  ,
  array
 (
 'country' = 'Singapore',
 )
  ,
  array
 (
 'country' = 'Singapore',
 )
  ,
  array
 (
 'country' = '',
 )
   ,
  array
 (
 'country' = '',
 )
,
array
 (
 'country' = '',
 )

  );


   function cmpcountry($a, $b)
   {

 $country1 = $a['country'];
 $country2 = $b['country'];

  return ($country1  $country2) ? -1 : 1;
   }

 usort($arraytest,cmpcountry);
 while(list($name,$value)=each($arraytest)){
   echo $name.brbr;

   while(list($n,$v) = each($arraytest[$name])){
   echo $v.brbr;
   }
  }



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting in array

2006-07-31 Thread Paul Novitski

At 10:31 PM 7/30/2006, weetat wrote:
 I have problem when doing usort() when 'country' = '', i would 
like to display records where country = '' last. Any ideas how to do that ?

...

  $arraytest= array(
 array
(
'country' = '',
)
 ,
 array
(
'country' = 'Thailand',
)

...

  function cmpcountry($a, $b)
  {
$country1 = $a['country'];
$country2 = $b['country'];

 return ($country1  $country2) ? -1 : 1;
  }



Weetat,

You can sort the blank fields to the end simply by forcing their 
evaluated values in your usort callback function:


  function cmpcountry($a, $b)
  {
$country1 = $a['country'];
$country2 = $b['country'];

if ($country1 == '') $country1 = zzz;
if ($country2 == '') $country2 = zzz;

return ($country1  $country2) ? -1 : 1;
  }

...or, using ternary syntax:

  function cmpcountry($a, $b)
  {
$country1 = ($a['country'] == '') ? zzz : $a['country'];
$country2 = ($b['country'] == '') ? zzz : $b['country'];

return ($country1  $country2) ? -1 : 1;
  }


Regards,
Paul 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] sorting in array

2006-07-31 Thread Paul Novitski

At 05:40 PM 7/30/2006, Peter Lauri wrote:

   function cmpcountry($a, $b)
   {

 $country1 = $a['country'];
 $country2 = $b['country'];

   if($country1=='') return 1;
   else return ($country1  $country2) ? -1 : 1;

   }



Good call, Peter; my suggestion was unnecessarily fat.

Weetat, both Peter's and my solutions do work -- see here:
http://juniperwebcraft.com/test/sortTest.php
source code:
http://juniperwebcraft.com/test/sortTest.txt

I could make that last statement just a bit simpler:

  function cmpcountry($a, $b)
  {
$country1 = ($a['country'] == '') ? zzz : $a['country'];
$country2 = ($b['country'] == '') ? zzz : $b['country'];

return ($country1  ''  $country1  $country2) ? -1 : 1;
  }

I'm curious to know why none of this code works properly if it 
directly compares ($a['country']  $b['country']).  Why are the 
intermediate variables necessary?


Paul 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] sorting in array

2006-07-31 Thread Paul Novitski

At 12:22 AM 7/31/2006, Paul Novitski wrote:

I could make that last statement just a bit simpler:

  function cmpcountry($a, $b)
  {
$country1 = ($a['country'] == '') ? zzz : $a['country'];
$country2 = ($b['country'] == '') ? zzz : $b['country'];

return ($country1  ''  $country1  $country2) ? -1 : 1;
  }



*Sigh*  I shouldn't post this late at night.  This is the comparison 
function that works for me:


  function cmpcountry($a, $b)
  {
$country1 = ($a['country'] == '') ? zzz : $a['country'];
$country2 = ($b['country'] == '') ? zzz : $b['country'];

return ($country1  $country2) ? -1 : 1;
  }

demo: http://juniperwebcraft.com/test/sortTest.php
code: http://juniperwebcraft.com/test/sortTest.txt

Paul  


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting in array

2006-07-31 Thread weetat

Thanks Paul,

  Very weird tried Peter's option, it doesn't work.

  Btw , how to sort by ascending ?

Thanks

Paul Novitski wrote:

At 12:22 AM 7/31/2006, Paul Novitski wrote:

I could make that last statement just a bit simpler:

  function cmpcountry($a, $b)
  {
$country1 = ($a['country'] == '') ? zzz : $a['country'];
$country2 = ($b['country'] == '') ? zzz : $b['country'];

return ($country1  ''  $country1  $country2) ? -1 : 1;
  }



*Sigh*  I shouldn't post this late at night.  This is the comparison 
function that works for me:


  function cmpcountry($a, $b)
  {
$country1 = ($a['country'] == '') ? zzz : $a['country'];
$country2 = ($b['country'] == '') ? zzz : $b['country'];

return ($country1  $country2) ? -1 : 1;
  }

demo: http://juniperwebcraft.com/test/sortTest.php
code: http://juniperwebcraft.com/test/sortTest.txt

Paul 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting in array

2006-07-31 Thread Paul Novitski

At 01:14 AM 7/31/2006, weetat wrote:

Thanks Paul,

  Very weird tried Peter's option, it doesn't work.

  Btw , how to sort by ascending ?



Please explain what you mean.  The current script DOES sort ascending 
by country (except for the blank country fields which are placed at the end):

http://juniperwebcraft.com/test/sortTest.php

Singapore, Singapore, Thailand is an ascending sort.

Your original code also sorted ascending by country, but with the 
blank countries at the front:


  function cmpcountry($a, $b)
  {
$country1 = $a['country'];
$country2 = $b['country'];

return ($country1  $country2) ? -1 : 1;
  }

Paul 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] sorting in array

2006-07-31 Thread Peter Lauri
And this for DESCending

   function cmpcountry($a, $b)
   {
 $country1 = $a['country'];
 $country2 = $b['country'];

if($country1=='' OR $country2=='') {
if($country1==$country2) return 0;
elseif($country1=='') return 1;
else return -1;
} else return ($country1  $country2) ? 1 : -1;
   }

-Original Message-
From: weetat [mailto:[EMAIL PROTECTED] 
Sent: Monday, July 31, 2006 3:15 PM
To: php-general@lists.php.net; Paul Novitski
Cc: php-general@lists.php.net
Subject: Re: [PHP] sorting in array

Thanks Paul,

   Very weird tried Peter's option, it doesn't work.

   Btw , how to sort by ascending ?

Thanks

Paul Novitski wrote:
 At 12:22 AM 7/31/2006, Paul Novitski wrote:
 I could make that last statement just a bit simpler:

   function cmpcountry($a, $b)
   {
 $country1 = ($a['country'] == '') ? zzz : $a['country'];
 $country2 = ($b['country'] == '') ? zzz : $b['country'];

 return ($country1  ''  $country1  $country2) ? -1 : 1;
   }
 
 
 *Sigh*  I shouldn't post this late at night.  This is the comparison 
 function that works for me:
 
   function cmpcountry($a, $b)
   {
 $country1 = ($a['country'] == '') ? zzz : $a['country'];
 $country2 = ($b['country'] == '') ? zzz : $b['country'];
 
 return ($country1  $country2) ? -1 : 1;
   }
 
 demo: http://juniperwebcraft.com/test/sortTest.php
 code: http://juniperwebcraft.com/test/sortTest.txt
 
 Paul 

-- 
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] sorting in array

2006-07-31 Thread Peter Lauri
Sorry, I just woke up and just posted an reply without thinking, use this:

   function cmpcountry($a, $b)
   {
 $country1 = $a['country'];
 $country2 = $b['country'];

if($country1=='' OR $country2=='') {
if($country1==$country2) return 0;
elseif($country1=='') return 1;
else return -1;
} else return ($country1  $country2) ? -1 : 1;
   }

/Peter



-Original Message-
From: Paul Novitski [mailto:[EMAIL PROTECTED] 
Sent: Monday, July 31, 2006 1:57 PM
To: php-general@lists.php.net
Subject: Re: [PHP] sorting in array

At 10:31 PM 7/30/2006, weetat wrote:
  I have problem when doing usort() when 'country' = '', i would 
 like to display records where country = '' last. Any ideas how to do that
?
...
   $arraytest= array(
  array
 (
 'country' = '',
 )
  ,
  array
 (
 'country' = 'Thailand',
 )
...
   function cmpcountry($a, $b)
   {
 $country1 = $a['country'];
 $country2 = $b['country'];

  return ($country1  $country2) ? -1 : 1;
   }


Weetat,

You can sort the blank fields to the end simply by forcing their 
evaluated values in your usort callback function:

   function cmpcountry($a, $b)
   {
 $country1 = $a['country'];
 $country2 = $b['country'];

 if ($country1 == '') $country1 = zzz;
 if ($country2 == '') $country2 = zzz;

 return ($country1  $country2) ? -1 : 1;
   }

...or, using ternary syntax:

   function cmpcountry($a, $b)
   {
 $country1 = ($a['country'] == '') ? zzz : $a['country'];
 $country2 = ($b['country'] == '') ? zzz : $b['country'];

 return ($country1  $country2) ? -1 : 1;
   }


Regards,
Paul 

-- 
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] sorting in array

2006-07-31 Thread David Tulloh
weetat wrote:
 Hi all ,
 
  I have array value as shown below, i have paste my test php code below:
  I have problem when doing usort() when 'country' = '', i would like to
 display records where country = '' last. Any ideas how to do that ?
 ...

You might try searching the list's archive's.  This question has been
asked and answered several times before.

http://aspn.activestate.com/ASPN/Mail/Message/php-general/3172783
http://aspn.activestate.com/ASPN/Mail/Message/php-general/3199536
http://aspn.activestate.com/ASPN/Mail/Message/php-general/3212898

one of the solutions offered:
http://aspn.activestate.com/ASPN/Mail/Message/3172873

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sorting in array

2006-07-30 Thread weetat

Hi all ,

 I have array value as shown below, i have paste my test php code below:
 I have problem when doing usort() when 'country' = '', i would like to 
display records where country = '' last. Any ideas how to do that ?


Thanks

  $arraytest= array(
 array
(
'country' = '',
)
 ,
 array
(
'country' = 'Thailand',
)
 ,
 array
(
'country' = 'Singapore',
)
 ,
 array
(
'country' = 'Singapore',
)
 ,
 array
(
'country' = '',
)
  ,
  array
(
'country' = '',
)
,
array
(
'country' = '',
)

 );


  function cmpcountry($a, $b)
  {

$country1 = $a['country'];
$country2 = $b['country'];

 return ($country1  $country2) ? -1 : 1;
  }

usort($arraytest,cmpcountry);
while(list($name,$value)=each($arraytest)){
   echo $name.brbr;

   while(list($n,$v) = each($arraytest[$name])){
   echo $v.brbr;
   }
 }

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] sorting in array

2006-07-30 Thread Peter Lauri
   function cmpcountry($a, $b)
   {

 $country1 = $a['country'];
 $country2 = $b['country'];

   if($country1=='') return 1;
   else return ($country1  $country2) ? -1 : 1;

   }

-Original Message-
From: weetat [mailto:[EMAIL PROTECTED] 
Sent: Monday, July 31, 2006 12:32 PM
To: php-general@lists.php.net
Subject: [PHP] sorting in array

Hi all ,

  I have array value as shown below, i have paste my test php code below:
  I have problem when doing usort() when 'country' = '', i would like to 
display records where country = '' last. Any ideas how to do that ?

Thanks

   $arraytest= array(
  array
 (
 'country' = '',
 )
  ,
  array
 (
 'country' = 'Thailand',
 )
  ,
  array
 (
 'country' = 'Singapore',
 )
  ,
  array
 (
 'country' = 'Singapore',
 )
  ,
  array
 (
 'country' = '',
 )
   ,
  array
 (
 'country' = '',
 )
,
array
 (
 'country' = '',
 )

  );


   function cmpcountry($a, $b)
   {

 $country1 = $a['country'];
 $country2 = $b['country'];

  return ($country1  $country2) ? -1 : 1;
   }

 usort($arraytest,cmpcountry);
 while(list($name,$value)=each($arraytest)){
   echo $name.brbr;

   while(list($n,$v) = each($arraytest[$name])){
   echo $v.brbr;
   }
  }

-- 
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] sorting associative array

2005-01-24 Thread Abdul-Wahid Paterson
Hi,

How about this


  foreach ($data as $key = $row) {
$scores[$key] = $row['scores'];
  }
 array_multisort($scores, SORT_ASC, $data);


Abdul-Wahid


On Mon, 24 Jan 2005 00:39:17 +1100, Jeffery Fernandez
[EMAIL PROTECTED] wrote:
 I have the following multi-dimentional array and I want to sort it by
 the score key value
 
 print_r($data);
 
 Array
 (
 [0] = Array
 (
 [video_id] = 71
 [old_id] = 7854
 [title] = When the fire comes
 [video_copies] = 1
 [running_time] = 48
 [date_published] = 06/02
 [place_published] = Australia
 [abstract] = ABC TV Gives details on many aspects of bushfires: 
 ...
 [library_medium_id] = 5
 [library_type] = 4
 [score] = 6.3310546875
 )
 
 [1] = Array
 (
 [video_id] = 9
 [old_id] = 7792
 [title] = Fire awareness
 [video_copies] = 1
 [running_time] = 15
 [date_published] =
 [place_published] = Victoria
 [abstract] = Safetycare Australia A general video lookin.
 [library_medium_id] = 5
 [library_type] = 4
 [score] = 3.1997931003571
 )
 
 )
 
 any ideas. Thanks
 
 Jeffery
 
 --
 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] sorting associative array

2005-01-24 Thread Jochem Maas
...

This won't work for me as I have 500+ records to sort based on the 
score key.. looking at jochem's class now. Thanks

which wont help much - assuming what you say is true
(why wont it work? have you tried it).
sort method from the class:
function sort()
{
   if(count($this-sortKeys)) {
   usort($this-dataArray, array($this,_sortcmp));
   }
}

Thanks jochem, I had a second look at it and I figured it out. I had 
problem implementing it with a class but now I have figured how to use it.

cheers,
Jeffery
ok, does that mean using usort() on your array works? my class is just a
fancy wrapper around usort - and ofcourse it allows you to sort on more than
1 'column' in the second dimension of the array (kind of like a multi-column
sort in an SQL statement). -- the point being that the usort() example Kurt
(I think that was his name) sent would work also.
anyway glad you could use it. :-)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sorting associative array

2005-01-24 Thread Jeffery Fernandez
Jochem Maas wrote:
...

This won't work for me as I have 500+ records to sort based on the 
score key.. looking at jochem's class now. Thanks


which wont help much - assuming what you say is true
(why wont it work? have you tried it).
sort method from the class:
function sort()
{
   if(count($this-sortKeys)) {
   usort($this-dataArray, array($this,_sortcmp));
   }
}

Thanks jochem, I had a second look at it and I figured it out. I had 
problem implementing it with a class but now I have figured how to 
use it.

cheers,
Jeffery

ok, does that mean using usort() on your array works? my class is just a
fancy wrapper around usort - and ofcourse it allows you to sort on 
more than
1 'column' in the second dimension of the array (kind of like a 
multi-column
sort in an SQL statement). -- the point being that the usort() example 
Kurt
(I think that was his name) sent would work also.

anyway glad you could use it. :-)

Yes the example sent by Kurt Yoder worked for me. I coudn't work out the 
errors with the class you sent me. I realised it was written for PHP5 in 
mind ?... or maybe I wasn't patient enough to spent time debugging it :-(

 /**
  * This function is to sort a multi-dimentional array. This is a 
call-back function.
  * Replace the value of $this-mArraySortKey with the appropriate key
  * before calling the call-back function
  */
 function sort_array($x, $y)
 {
   if ( $x[$this-mArraySortKey] == $y[$this-mArraySortKey] )
 return 0;
   else if ( $x[$this-mArraySortKey]  $y[$this-mArraySortKey] )
 return 1;
   else
 return -1;
 }

and within my class I am calling the following two lines:
   // Sort the array
   $this-mArraySortKey = 'score';
   usort($this-mSearchData, array( $this, 'sort_array'));
cheers,
Jeffery Fernandez
http://melbourne.ug.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: [PHP] sorting associative array

2005-01-24 Thread Ford, Mike
To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm



On 23 January 2005 22:37, Jeffery Fernandez wrote:

 Kurt Yoder wrote:
 
  Use usort (stealing from php docs):
  
  
  
  function cmp($a['score'], $b['score'])

That should be:

function cmp($a, $b)

  {
 if ($a['score'] == $b['score']) {
 return 0;
 }
 return ($a['score']  $b['score']) ? -1 : 1;
  }
  
  $data = array(...);
  
  usort($data, cmp);
  
  
 This won't work for me as I have 500+ records to sort based on the
 score key..

Sure it will, given the correction above.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, 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] sorting associative array

2005-01-24 Thread Jochem Maas
Jeffery Fernandez wrote:
Jochem Maas wrote:
...

Yes the example sent by Kurt Yoder worked for me. I coudn't work out the 
errors with the class you sent me. I realised it was written for PHP5 in 
mind ?... or maybe I wasn't patient enough to spent time debugging it :-(

I did change it for php5 (to get rid of E_STRICT warnings IIR)
- but the change was fairly cosmetic:
the class def starts like:
class MDASort {
private $dataArray; //the array we want to sort.
private $sortKeys;  //the order in which we want the array to be sorted.
if you change that to:
class MDASort {
var $dataArray; //the array we want to sort.
var $sortKeys;  //the order in which we want the array to be sorted.
then the class should work under php4 as advertised (adding an '' as you
do below in the callback definition wouldn't hurt either).
...
and within my class I am calling the following two lines:
   // Sort the array
   $this-mArraySortKey = 'score';
   usort($this-mSearchData, array( $this, 'sort_array'));

cool, taking apart someone elses code and rewriting it one of the best ways
of learning/understanding IMHO. btw the '' before $this is not strictly 
required,
but it should save you a few cycles :-)

cheers,
Jeffery Fernandez
http://melbourne.ug.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sorting associative array

2005-01-24 Thread Jeffery Fernandez
Jochem Maas wrote:
Jeffery Fernandez wrote:
Jochem Maas wrote:
...

Yes the example sent by Kurt Yoder worked for me. I coudn't work out 
the errors with the class you sent me. I realised it was written for 
PHP5 in mind ?... or maybe I wasn't patient enough to spent time 
debugging it :-(

I did change it for php5 (to get rid of E_STRICT warnings IIR)
- but the change was fairly cosmetic:
the class def starts like:
class MDASort {
private $dataArray; //the array we want to sort.
private $sortKeys;  //the order in which we want the array to be 
sorted.

if you change that to:
class MDASort {
var $dataArray; //the array we want to sort.
var $sortKeys;  //the order in which we want the array to be sorted.
then the class should work under php4 as advertised (adding an '' as you
do below in the callback definition wouldn't hurt either).
...
and within my class I am calling the following two lines:
   // Sort the array
   $this-mArraySortKey = 'score';
   usort($this-mSearchData, array( $this, 'sort_array'));

cool, taking apart someone elses code and rewriting it one of the best 
ways
of learning/understanding IMHO. btw the '' before $this is not 
strictly required,
but it should save you a few cycles :-)

You always learn by re-writing someone elses code ;-)
I had done the changes to the variable declaration and also changed the 
constructor name to be the name of the class and it still gave me 
errors. Perhaps I will test it without any Error reporting on... needs 
some tweaking I guess.

cheers,
Jeffery Fernandez
http://melbourne.ug.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] sorting associative array

2005-01-23 Thread Jeffery Fernandez
I have the following multi-dimentional array and I want to sort it by 
the score key value

print_r($data);
Array
(
   [0] = Array
   (
   [video_id] = 71
   [old_id] = 7854
   [title] = When the fire comes
   [video_copies] = 1
   [running_time] = 48
   [date_published] = 06/02
   [place_published] = Australia
   [abstract] = ABC TV Gives details on many aspects of bushfires: 
...
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 6.3310546875
   )
   [1] = Array
   (
   [video_id] = 9
   [old_id] = 7792
   [title] = Fire awareness
   [video_copies] = 1
   [running_time] = 15
   [date_published] = 
   [place_published] = Victoria
   [abstract] = Safetycare Australia A general video lookin.
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 3.1997931003571
   )

)
any ideas. Thanks
Jeffery
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sorting associative array

2005-01-23 Thread Jochem Maas
Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it by 
the score key value
...
any ideas. Thanks
A long time ago I had this problem, came up with the following class (based 
on
someone else's stuff that I found in the comments section of the php manual) to
handle it for me. its probably a rubbish way of doing it (look forward to
some clever bar steward showing us how do it in 3 lines. :-) ) - there are 
usage examples
at the bottom:
?php
/**
 * MDASort.class.php :: simple interface to sort a mutlidimensional array
 *
 * We often have arrays of arrays where the sub-arrays are rows of data
 * (either created or extracted from a database) - this class allows us to
 * easily sort arrays in the base container array by any number of keys found
 * in the sub-arrays. be aware that it is assumed that the array keys found in 
the
 * sub arrays are associative. Also maybe the _sortcmp method could be enhanced 
to
 * allow arbitrary levels of arrays to be sorted: by calling sort() on a level
 * N array also the sortKeys contents would then have to be checked to see if
 * they applied to the current (sub-)array
 *
 * @author  Some guy on PHP comment board. http://www.php.net/usort\
 * @author  Jochem Maas [EMAIL PROTECTED]
 *
 * $Id: MDASort.class.php,v 1.1 2004/08/03 13:38:37 jochem Exp $
 *
 */
/**
 * This file and its contents is not copyrighted;
 * The contents are free to be used by anybody under any conditions.
 */
class MDASort {
private $dataArray; //the array we want to sort.
private $sortKeys;  //the order in which we want the array to be sorted.
function __construct()
{
if ($cnt = func_num_args()) {
$args = func_get_args();
if (isset($args[0])) {
$this-setData($args[0]);
}
if (isset($args[1])) {
$this-setSortKeys($args[1]);
}
if (isset($args[2])  $args[2] === true) {
$this-sort();
}
}
}
function _sortcmp($a, $b, $i=0)
{
   $r = strnatcmp($a[$this-sortKeys[$i][0]],$b[$this-sortKeys[$i][0]]);
   if ($this-sortKeys[$i][1] == DESC) $r = $r * -1;
   if($r==0) {
   $i++;
   if ($this-sortKeys[$i]) $r = $this-_sortcmp($a, $b, $i);
   }
   return $r;
}
function sort()
{
   if(count($this-sortKeys)) {
   usort($this-dataArray, array($this,_sortcmp));
   }
}
function setData($dataArray = array())
{
$this-dataArray = $dataArray;
}
function setSortKeys($sortKeys = array())
{
$this-sortKeys = $sortKeys;
}
function getData()
{
return $this-dataArray;
}
function getSortKeys()
{
return $this-sortKeys;
}
}
/* example of usage */
/*
$sorter = new MDASort;
$sorter-setData( array(
   array(name = hank, headsize = small, age = 32),
   array(name = sade, headsize = petit, age = 36),
   array(name = hank, headsize = large, age = 33),
   array(name = sade, headsize = large, age = 32),
   array(name = john, headsize = large, age = 32),
   array(name = hank, headsize = small, age = 36),
   array(name = hank, headsize = small, age = 40)
));
$sorter-setSortKeys( array(
   array('name','ASC'),
   array('headsize','DESC'),
   array('age','ASC'),
));
$sorter-sort();
$sortedArray = $sorter-getData();
*/
/* 2nd example of usage */
/*
$data = array(
   array(name = hank, headsize = small, age = 32),
   array(name = sade, headsize = petit, age = 36),
   array(name = hank, headsize = large, age = 33),
   array(name = sade, headsize = large, age = 32),
   array(name = john, headsize = large, age = 32),
   array(name = hank, headsize = small, age = 36),
   array(name = hank, headsize = small, age = 40)
);
$sort = array(
   array('name','ASC'),
   array('headsize','DESC'),
   array('age','ASC'),
);
$sorter = new MDASort($data, $sort);
$sorter-sort();
$sortedArray = $sorter-getData();
*/
/* 3rd example of usage */
/*
$data = array(
   array(name = hank, headsize = small, age = 32),
   array(name = sade, headsize = petit, age = 36),
   array(name = hank, headsize = large, age = 33),
   array(name = sade, headsize = large, age = 32),
   array(name = john, headsize = large, age = 32),
   array(name = hank, headsize = small, age = 36),
   array(name = hank, headsize = small, age = 40)
);
$sort = array(
   array('name','ASC'),
   array('headsize','DESC'),
   array('age','ASC'),
);
$sorter = new MDASort($data, $sort, true);  // auto sort
$sortedArray = $sorter-getData();
*/
Jeffery
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sorting associative array

2005-01-23 Thread Kurt Yoder
Use usort (stealing from php docs):

function cmp($a['score'], $b['score'])
{
   if ($a['score'] == $b['score']) {
   return 0;
   }
   return ($a['score']  $b['score']) ? -1 : 1;
}
$data = array(...);
usort($data, cmp);

On Jan 23, 2005, at 8:39 AM, Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it by 
the score key value

print_r($data);
Array
(
   [0] = Array
   (
   [video_id] = 71
   [old_id] = 7854
   [title] = When the fire comes
   [video_copies] = 1
   [running_time] = 48
   [date_published] = 06/02
   [place_published] = Australia
   [abstract] = ABC TV Gives details on many aspects of 
bushfires: ...
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 6.3310546875
   )

   [1] = Array
   (
   [video_id] = 9
   [old_id] = 7792
   [title] = Fire awareness
   [video_copies] = 1
   [running_time] = 15
   [date_published] =[place_published] = Victoria
   [abstract] = Safetycare Australia A general video 
lookin.
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 3.1997931003571
   )

)
any ideas. Thanks
Jeffery
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
Kurt Yoder
http://yoderhome.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sorting associative array

2005-01-23 Thread Jeffery Fernandez
Kurt Yoder wrote:
Use usort (stealing from php docs):

function cmp($a['score'], $b['score'])
{
   if ($a['score'] == $b['score']) {
   return 0;
   }
   return ($a['score']  $b['score']) ? -1 : 1;
}
$data = array(...);
usort($data, cmp);

This won't work for me as I have 500+ records to sort based on the score 
key.. looking at jochem's class now. Thanks

Jeffery
On Jan 23, 2005, at 8:39 AM, Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it by 
the score key value

print_r($data);
Array
(
   [0] = Array
   (
   [video_id] = 71
   [old_id] = 7854
   [title] = When the fire comes
   [video_copies] = 1
   [running_time] = 48
   [date_published] = 06/02
   [place_published] = Australia
   [abstract] = ABC TV Gives details on many aspects of 
bushfires: ...
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 6.3310546875
   )

   [1] = Array
   (
   [video_id] = 9
   [old_id] = 7792
   [title] = Fire awareness
   [video_copies] = 1
   [running_time] = 15
   [date_published] =[place_published] = Victoria
   [abstract] = Safetycare Australia A general video 
lookin.
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 3.1997931003571
   )

)
any ideas. Thanks
Jeffery
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
Kurt Yoder
http://yoderhome.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sorting associative array

2005-01-23 Thread Jochem Maas
Jeffery Fernandez wrote:
Kurt Yoder wrote:
Use usort (stealing from php docs):

function cmp($a['score'], $b['score'])
{
   if ($a['score'] == $b['score']) {
   return 0;
   }
   return ($a['score']  $b['score']) ? -1 : 1;
}
$data = array(...);
usort($data, cmp);

This won't work for me as I have 500+ records to sort based on the score 
key.. looking at jochem's class now. Thanks
which wont help much - assuming what you say is true
(why wont it work? have you tried it).
sort method from the class:
function sort()
{
   if(count($this-sortKeys)) {
   usort($this-dataArray, array($this,_sortcmp));
   }
}

Jeffery
On Jan 23, 2005, at 8:39 AM, Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it by 
the score key value

print_r($data);
Array
(
   [0] = Array
   (
   [video_id] = 71
   [old_id] = 7854
   [title] = When the fire comes
   [video_copies] = 1
   [running_time] = 48
   [date_published] = 06/02
   [place_published] = Australia
   [abstract] = ABC TV Gives details on many aspects of 
bushfires: ...
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 6.3310546875
   )

   [1] = Array
   (
   [video_id] = 9
   [old_id] = 7792
   [title] = Fire awareness
   [video_copies] = 1
   [running_time] = 15
   [date_published] =[place_published] = Victoria
   [abstract] = Safetycare Australia A general video 
lookin.
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 3.1997931003571
   )

)
any ideas. Thanks
Jeffery
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
Kurt Yoder
http://yoderhome.com


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] sorting associative array

2005-01-23 Thread Jeffery Fernandez
Jochem Maas wrote:
Jeffery Fernandez wrote:
Kurt Yoder wrote:
Use usort (stealing from php docs):

function cmp($a['score'], $b['score'])
{
   if ($a['score'] == $b['score']) {
   return 0;
   }
   return ($a['score']  $b['score']) ? -1 : 1;
}
$data = array(...);
usort($data, cmp);

This won't work for me as I have 500+ records to sort based on the 
score key.. looking at jochem's class now. Thanks

which wont help much - assuming what you say is true
(why wont it work? have you tried it).
sort method from the class:
function sort()
{
   if(count($this-sortKeys)) {
   usort($this-dataArray, array($this,_sortcmp));
   }
}

Thanks jochem, I had a second look at it and I figured it out. I had 
problem implementing it with a class but now I have figured how to use it.

cheers,
Jeffery

Jeffery
On Jan 23, 2005, at 8:39 AM, Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it 
by the score key value

print_r($data);
Array
(
   [0] = Array
   (
   [video_id] = 71
   [old_id] = 7854
   [title] = When the fire comes
   [video_copies] = 1
   [running_time] = 48
   [date_published] = 06/02
   [place_published] = Australia
   [abstract] = ABC TV Gives details on many aspects of 
bushfires: ...
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 6.3310546875
   )

   [1] = Array
   (
   [video_id] = 9
   [old_id] = 7792
   [title] = Fire awareness
   [video_copies] = 1
   [running_time] = 15
   [date_published] =[place_published] = 
Victoria
   [abstract] = Safetycare Australia A general video 
lookin.
   [library_medium_id] = 5
   [library_type] = 4
   [score] = 3.1997931003571
   )

)
any ideas. Thanks
Jeffery
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
Kurt Yoder
http://yoderhome.com



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] Sorting multidim array and keeping associations

2004-11-17 Thread Peter Lauri
Best groupmember,

I have an multidim array that looks something like this:

[40] = [1]=32, [2]=55, [total]=87
[22] = [8]=2, [7]=105, [total]=107
[142] = [2]=3, [7]=8, [total]=11

I want to sort this array according to the total and still keep the acc.
with the basekey. I know I can easily do this by writing an function myself.
But are there any built in functions for this? I have looked at usort,
uksort and more, but they do not seem to solve my problem.

- Best Of Times
/Peter

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Sorting multidim array and keeping associations

2004-11-17 Thread Peter Lauri
Best groupmember,

I have an multidim array that looks something like this:

[40] = [1]=32, [2]=55, [total]=87
[22] = [8]=2, [7]=105, [total]=107
[142] = [2]=3, [7]=8, [total]=11

I want to sort this array according to the total and still keep the acc.
with the basekey. I know I can easily do this by writing an function myself.
But are there any built in functions for this? I have looked at usort,
uksort and more, but they do not seem to solve my problem.

- Best Of Times
/Peter

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sorting multidimensional array by a second level value

2004-09-17 Thread Chris Lott
I have an array $links like this:

[1] = Array
(
[href] = http://www.poetrymagazine.org/epstein_sept_prose.html
[description] = Thank You, No
[time] = 2004-09-17T17:30:32Z
)

[2] = Array
(
[href] = http://110am.com/projects/manuscript/
[description] = Manuscript
[time] = 2004-09-16T19:25:14Z
)

I'd like to sort the array based on one of the values in the field
href, description, or time. Is there a canonical way of doing this?

c
-- 
Chris Lott

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting multidimensional array by a second level value

2004-09-17 Thread Jason Davidson
http://ca3.php.net/manual/en/function.array-multisort.php

this might do it.

Jason

Chris Lott [EMAIL PROTECTED] wrote: 
 
 I have an array $links like this:
 
 [1] = Array
 (
 [href] = http://www.poetrymagazine.org/epstein_sept_prose.html
 [description] = Thank You, No
 [time] = 2004-09-17T17:30:32Z
 )
 
 [2] = Array
 (
 [href] = http://110am.com/projects/manuscript/
 [description] = Manuscript
 [time] = 2004-09-16T19:25:14Z
 )
 
 I'd like to sort the array based on one of the values in the field
 href, description, or time. Is there a canonical way of doing this?
 
 c
 -- 
 Chris Lott
 
 -- 
 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] sorting multidimensional array by a second level value

2004-09-17 Thread Steve Brown
 I'd like to sort the array based on one of the values in the field
 href, description, or time. Is there a canonical way of doing this?

Probably easiest to write your own sort function the use usort(), 
http://www.php.net/usort has pretty good documentation on how to
accomplish this.

Steve

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting multidimensional array by a second level value

2004-09-17 Thread Matt M.
  I'd like to sort the array based on one of the values in the field
  href, description, or time. Is there a canonical way of doing this?

you might be able to use http://us2.php.net/manual/en/function.usort.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array..dont know numeric or alpha-numeric

2004-08-07 Thread Kevin Waterson
This one time, at band camp, PHP Gen [EMAIL PROTECTED] wrote:

 Hi,
 I have a function that reads jpg files (thumbnails)
 from a directory and puts all the files names into an
 array...I want to sort that array by the filename,
 problem is, I dont know if filenames will be pure
 numeric (eg 001.jpg,002.jpg) or alpha-numeric
 (asdf001,asdf002)


?php 
  $hdl = opendir('./'); 
  while ($dirEntry = readdir($hdl)) { 
if (substr($dirEntry, 0, 1) != '.') {
  $listing[] = $dirEntry; 
  }
}
  natsort($listing);
  closedir($hdl);
  foreach($listing as $blah) { echo $blah.'br /'; }
?

or something.

Kevin

-- 
 __  
(_ \ 
 _) )            
|  /  / _  ) / _  | / ___) / _  )
| |  ( (/ / ( ( | |( (___ ( (/ / 
|_|   \) \_||_| \) \)
Kevin Waterson
Port Macquarie, Australia

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sorting an array..dont know numeric or alpha-numeric

2004-08-06 Thread PHP Gen
Hi,
I have a function that reads jpg files (thumbnails)
from a directory and puts all the files names into an
array...I want to sort that array by the filename,
problem is, I dont know if filenames will be pure
numeric (eg 001.jpg,002.jpg) or alpha-numeric
(asdf001,asdf002)

It HAS to be sequential from small to big (eg: 0-10)
as I am making static html pages from them further
down the program.

Looking in the manual I have tried sort() without any
luck, then looking further I found natcasesort() which
would be perfect for my needs right now, but cant get
it to work :-( 

Below is the function (its not big)

*** Start function 

function directory($dir,$filters){
$handle=opendir($dir);
$files=array();
if ($filters == all){while(($file =
readdir($handle))!==false){$files[] = $file;}}
if ($filters != all){
$filters=explode(,,$filters);
while (($file = readdir($handle))!==false) {
for ($f=0;$fsizeof($filters);$f++):
$system=explode(.,$file);
if ($system[1] == $filters[$f]){$files[] = $file;}
endfor;
}
}
closedir($handle);
return $files;
}




$pics=directory(pics,jpg,JPG,JPEG,jpeg,png,PNG);

*** End function 

I have tried putting natcasesort() in many places but
not working

Thanks in advance.
-Mag

=
--
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)



__
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array..dont know numeric or alpha-numeric

2004-08-06 Thread Curt Zirzow
* Thus wrote PHP Gen:
...
 
 Looking in the manual I have tried sort() without any
 luck, then looking further I found natcasesort() which
 would be perfect for my needs right now, but cant get
 it to work :-( 
 
 Below is the function (its not big)
 
 *** Start function 
 
 function directory($dir,$filters){
   $handle=opendir($dir);
   $files=array();
   if ($filters == all){while(($file =

For starters, if your going to provide some code, make sure its
readable by others.. trying to figure out what it is doing is
nearly impossible the way it is written..

function directory($dir,$filters){

  $handle=opendir($dir);
  $files=array();

  if ($filters == all){

while(($file = readdir($handle)) !== false) { 
  $files[] = $file;
}

  }

  if ($filters != all) {
$filters=explode(,,$filters);

while (($file = readdir($handle)) !== false) {

  for ($f=0;$fsizeof($filters);$f++) {
$system=explode(.,$file);

if ($system[1] == $filters[$f]) {
  $files[] =+ $file;
}

  }

}
  }

  closedir($handle);

  return $files;
}

ahh.. much better, but before we go on.. some redefining of the
function: 

/* if no filter provided, this will
 * return all files in the directory */
function directory($dir,$filters=''){

  $handle=opendir($dir);
  if (! $handle ) { /* better check this!! */
return false;
  }

  $files=array();

  /* I may have extension I want that ends in .all 
   * function is redefined to reflect this */

  //if ($filters == all)
  if (! $filters){

while(($file = readdir($handle)) !== false) { 
  $files[] = $file;
}

  } else {
/* Don't need to re-evaluate $filters since this
 * is an if else situation. */

//$filters=explode(,,$filters);
/* not wise to change the meaning of a variable */

$filter_list = explode(',', $filters);

while (($file = readdir($handle)) !== false) {

  /* ouch..!  this is rather expensive, in a tight loop
   * always try to have the php engine do the filtering
   * in situations like this. see after /intensive code..
   
  for ($f=0;$fsizeof($filters);$f++) {
$system=explode(.,$file);

// note: this is assuming that there is only one dot
if ($system[1] == $filters[$f]) {

  // I'm assuming =+ was by accident, should've been =
  $files[] =+ $file;

}
  }
  // end intensive code
  */

  // instead... simple extension finder
  if ($dot = strrpos($file, '.') !== false) {

// and see if it passes the filter
if (in_array(substr($file, $dot+1), $filter_list)) {
  $files[] = $file;
}
// note: preg_match'ing a pattern would probably be most
// optimal, since the pattern is cached.
  }

}
  }

  closedir($handle);

  return $files;
}

 $pics=directory(pics,jpg,JPG,JPEG,jpeg,png,PNG);
 
 *** End function 

 
 I have tried putting natcasesort() in many places but
 not working

oh. well.. how were you trying natcasesort()?

With the example provided in the manual:

$array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png',
'img2.png', 'img1.png', 'IMG3.png');

//...

natcasesort($array2);
print_r($array2);

should help.


Curt
-- 
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about.  No, sir.  Our model is the trapezoid!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array..dont know numeric or alpha-numeric

2004-08-06 Thread PHP Gen
Hi Curt,

Damn, looks like I (unintentionally) gave you guys
quite a challenge! You're the first one to reply and
looks like you sure worked on it!

 For starters, if your going to provide some code,
 make sure its
 readable by others.. trying to figure out what it is
 doing is
 nearly impossible the way it is written..

Sorry about that, I didnt write the code, I snipped
it off from phpbuilder (i think, might have been
another php help site) and since it was working I
didnt touch or do anything.

Will go through the modifications and advise you gave
me and will write back if.. any problems.

Thanks again,
-Mag

=
--
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)



__
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Sorting an array

2004-06-02 Thread John Nichel
Okay, I know someone is going to shoot me for asking such a dumb 
question, but I just can't seem to find the answer anywhere.  I have a 
multidimensional array which I would like to sort on the value of one of 
the keys...

Array
(
[0] = Array
(
[foo] = blah
[bar] = blah
[sort] = 5
)
[1] = Array
(
[foo] = blah
[bar] = blah
[sort] = 2
)
[2] = Array
(
[foo] = blah
[bar] = blah
[sort] = 4
)
[3] = Array
(
[foo] = blah
[bar] = blah
[sort] = 3
)
[4] = Array
(
[foo] = blah
[bar] = blah
[sort] = 1
)
)
What I would like to do is sort this on the value of 'sort' in each 
sub-array.  What am I missing  TIA

--
John C. Nichel
KegWorks.com
716.856.9675
[EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Sorting an array

2004-06-02 Thread Robert Cummings
On Wed, 2004-06-02 at 12:59, John Nichel wrote:
 Okay, I know someone is going to shoot me for asking such a dumb 
 question, but I just can't seem to find the answer anywhere.  I have a 
 multidimensional array which I would like to sort on the value of one of 
 the keys...
 
 Array
 (
  [0] = Array
  (
  [foo] = blah
  [bar] = blah
  [sort] = 5
   )
  [1] = Array
  (
  [foo] = blah
  [bar] = blah
  [sort] = 2
   )
  [2] = Array
  (
  [foo] = blah
  [bar] = blah
  [sort] = 4
   )
  [3] = Array
  (
  [foo] = blah
  [bar] = blah
  [sort] = 3
   )
  [4] = Array
  (
  [foo] = blah
  [bar] = blah
  [sort] = 1
   )
 )
 
 What I would like to do is sort this on the value of 'sort' in each 
 sub-array.  What am I missing  TIA

usort( $theAboveArray, 'mySortHandler' );

function mySortHandler( $v1, $v2 )
{
if( $v1['sort']  $v2['sort'] )
{
return 1;
}
else
if( $v1['sort']  $v2['sort'] )
{
return -1;
}

return 0;
}

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Sorting an array

2004-06-02 Thread Jay Blanchard
[snip]
What I would like to do is sort this on the value of 'sort' in each 
sub-array.  What am I missing  TIA
[/snip]

http://us4.php.net/manual/en/function.array-multisort.php

HTH! :)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sorting an array

2004-06-02 Thread John Nichel
Robert Cummings wrote:
On Wed, 2004-06-02 at 12:59, John Nichel wrote:
What I would like to do is sort this on the value of 'sort' in each 
sub-array.  What am I missing  TIA

usort( $theAboveArray, 'mySortHandler' );
function mySortHandler( $v1, $v2 )
{
if( $v1['sort']  $v2['sort'] )
{
return 1;
}
else
if( $v1['sort']  $v2['sort'] )
{
return -1;
}
return 0;
}
Cheers,
Rob.
That did the trick.  Thanks.
--
John C. Nichel
KegWorks.com
716.856.9675
[EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] sorting an array of regexes

2003-11-18 Thread Adam i Agnieszka Gasiorowski FNORD

There is an array of regexes, for example

 $array = array('moon', '[wh]ood', '[^as]eed' ...
 (about 300 entries). 

I want to sort it comparing to the
 character lenght of a regex. For example
 [wh]ood is 4 characters, moon is 4 characters.
 There are only letters of the alphabet and
 letter ranges present in those regexes. I
 want the longest ones first.

How would you write the sorting function?

-- 
Seks, seksi, seksolatki... news:pl.soc.seks.moderowana
http://hyperreal.info  { iWanToDie }   WiNoNa)   (
http://szatanowskie-ladacznice.0-700.pl  foReVeR(  *  )
Poznaj jej zwiewne ksztaty... http://www.opera.com 007

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array of regexes

2003-11-18 Thread Eugene Lee
On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka Gasiorowski FNORD wrote:
: 
:   There is an array of regexes, for example
: 
:  $array = array('moon', '[wh]ood', '[^as]eed' ...
:  (about 300 entries). 
: 
:   I want to sort it comparing to the
:  character lenght of a regex. For example
:  [wh]ood is 4 characters, moon is 4 characters.
:  There are only letters of the alphabet and
:  letter ranges present in those regexes. I
:  want the longest ones first.
: 
:   How would you write the sorting function?

This might be the most functionally correct, although it's definitely
not the fastest route.

function re_len($pat)
{
return strlen(preg_replace('/\[[^]]+]/', '_', $pat));
}

function re_sort($a_pat, $b_pat)
{
$a = re_len($a_pat);
$b = re_len($b_pat);
if ($a == $b)
{
return 0;
}
return ($a  $b ) ? -1 : 1;
}

usort($array, 're_sort');

BTW, re_len() will bomb on certain oddball patterns with strange ranges.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] sorting an array of regexes

2003-11-18 Thread Wouter van Vliet
 -Oorspronkelijk bericht-
 Van: Eugene Lee [mailto:[EMAIL PROTECTED]
 On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka
 Gasiorowski FNORD wrote:
 :
 : There is an array of regexes, for example
 :
 :  $array = array('moon', '[wh]ood', '[^as]eed' ...
 :  (about 300 entries).
 :
 : I want to sort it comparing to the
 :  character lenght of a regex. For example
 :  [wh]ood is 4 characters, moon is 4 characters.
 :  There are only letters of the alphabet and
 :  letter ranges present in those regexes. I
 :  want the longest ones first.
 :
 : How would you write the sorting function?

 This might be the most functionally correct, although it's definitely
 not the fastest route.

   function re_len($pat)
   {
   return strlen(preg_replace('/\[[^]]+]/', '_', $pat));

I think you meant:

/\[[^\]]+]/

as regex ;) Not sure, but I think one more block-bracked needed to be
escaped ;)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array of regexes

2003-11-18 Thread Adam i Agnieszka Gasiorowski FNORD
Eugene Lee wrote:
 
 On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka Gasiorowski FNORD wrote:
 :
 :   There is an array of regexes, for example
 :
 :  $array = array('moon', '[wh]ood', '[^as]eed' ...
 :  (about 300 entries).
 :
 :   I want to sort it comparing to the
 :  character lenght of a regex. For example
 :  [wh]ood is 4 characters, moon is 4 characters.
 :  There are only letters of the alphabet and
 :  letter ranges present in those regexes. I
 :  want the longest ones first.
 :
 :   How would you write the sorting function?
 
 This might be the most functionally correct, although it's definitely
 not the fastest route.

 Thank you, that will certainly work :8]. Does
 anyone have any thoughts how to make it faster? It is
 not VERY critical, because the calculation will be
 done only once, at initialization, but...well, you 
 know :8].

* * *

 How about...if I count the number of ']'
 in string and then add it to the strlen - (the
 number of ']' x 2)? There are no [ nor ] in these
 except in the character range parts. Does that
 look faster than applying regular expression?
 
 function re_len($pat)
 {
 return strlen(preg_replace('/\[[^]]+]/', '_', $pat));
 }
 
 function re_sort($a_pat, $b_pat)
 {
 $a = re_len($a_pat);
 $b = re_len($b_pat);
 if ($a == $b)
 {
 return 0;
 }
 return ($a  $b ) ? -1 : 1;
 }
 
 usort($array, 're_sort');
 
 BTW, re_len() will bomb on certain oddball patterns with strange ranges.

Like? Sorry, I can't think of anything
 right now...

-- 
Seks, seksi, seksolatki... news:pl.soc.seks.moderowana
http://hyperreal.info  { iWanToDie }   WiNoNa)   (
http://szatanowskie-ladacznice.0-700.pl  foReVeR(  *  )
Poznaj jej zwiewne ksztaty... http://www.opera.com 007

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array of regexes

2003-11-18 Thread Curt Zirzow
* Thus wrote Adam i Agnieszka Gasiorowski FNORD ([EMAIL PROTECTED]):
 Eugene Lee wrote:
  
  On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka Gasiorowski FNORD wrote:
  :
  :   There is an array of regexes, for example
  :
  :  $array = array('moon', '[wh]ood', '[^as]eed' ...
  :  (about 300 entries).
  :
  :   I want to sort it comparing to the
  :  character lenght of a regex. For example
  :  [wh]ood is 4 characters, moon is 4 characters.
  :  There are only letters of the alphabet and
  :  letter ranges present in those regexes. I
  :  want the longest ones first.
  :
  :   How would you write the sorting function?
  
  This might be the most functionally correct, although it's definitely
  not the fastest route.
 
  Thank you, that will certainly work :8]. Does
  anyone have any thoughts how to make it faster? It is
  not VERY critical, because the calculation will be
  done only once, at initialization, but...well, you 
  know :8].

Do these change all the time or are they rather static?  I would
suggest using this routine to generate the list to be used in your
program, instead of doing this on the fly.


Curt
-- 
My PHP key is worn out

  PHP List stats since 1997: 
http://zirzow.dyndns.org/html/mlists/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array of regexes

2003-11-18 Thread Eugene Lee
On Tue, Nov 18, 2003 at 01:52:39PM +0100, Wouter van Vliet wrote:
: Eugene Lee suggested:
:  On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka
:  Gasiorowski FNORD wrote:
:  :
:  :   There is an array of regexes, for example
:  :
:  :  $array = array('moon', '[wh]ood', '[^as]eed' ...
:  :  (about 300 entries).
:  :
:  :   I want to sort it comparing to the
:  :  character lenght of a regex. For example
:  :  [wh]ood is 4 characters, moon is 4 characters.
:  :  There are only letters of the alphabet and
:  :  letter ranges present in those regexes. I
:  :  want the longest ones first.
:  :
:  :   How would you write the sorting function?
: 
:  This might be the most functionally correct, although it's definitely
:  not the fastest route.
: 
:  function re_len($pat)
:  {
:  return strlen(preg_replace('/\[[^]]+]/', '_', $pat));
: 
: I think you meant:
: 
:   /\[[^\]]+]/
: 
: as regex ;) Not sure, but I think one more block-bracked needed to be
: escaped ;)

Nope.  My pattern is legitimate.  Within a range, if the first character
is a closing-square-bracket ']', it is treated as the literal character
and not as the end of range.  If the range starts with a negation '^',
then the same rule applies to the second character.

This is also a sad indication that I really know my regular expressions,
or I need a vacation.  :-)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array of regexes

2003-11-18 Thread Adam i Agnieszka Gasiorowski FNORD
Curt Zirzow wrote:
 
 * Thus wrote Adam i Agnieszka Gasiorowski FNORD ([EMAIL PROTECTED]):
  Eugene Lee wrote:
 
   On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka Gasiorowski FNORD 
   wrote:
   :
   :   There is an array of regexes, for example
   :
   :  $array = array('moon', '[wh]ood', '[^as]eed' ...
   :  (about 300 entries).
   :
   :   I want to sort it comparing to the
   :  character lenght of a regex. For example
   :  [wh]ood is 4 characters, moon is 4 characters.
   :  There are only letters of the alphabet and
   :  letter ranges present in those regexes. I
   :  want the longest ones first.
   :
   :   How would you write the sorting function?
  
   This might be the most functionally correct, although it's definitely
   not the fastest route.
 
   Thank you, that will certainly work :8]. Does
   anyone have any thoughts how to make it faster? It is
   not VERY critical, because the calculation will be
   done only once, at initialization, but...well, you
   know :8].
 
 Do these change all the time or are they rather static?  I would
 suggest using this routine to generate the list to be used in your
 program, instead of doing this on the fly.

Good idea...

-- 
Seks, seksi, seksolatki... news:pl.soc.seks.moderowana
http://hyperreal.info  { iWanToDie }   WiNoNa)   (
http://szatanowskie-ladacznice.0-700.pl  foReVeR(  *  )
Poznaj jej zwiewne ksztaty... http://www.opera.com 007

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Sorting an array by value length.

2003-10-22 Thread Rolf Brusletto
Hey all -

I've been scouring the php.net site for a few hours now trying to find a 
viable way to sort a single dimensional array( array('0'='fddfsdsfdds', 
'1'=','d','2' = 'gofofle'); ). so that the longest lengthed 
item is at the top, and the smallest lengthed item is at the bottom 
is there a function to do this... example:

$arrayToSort = array('0' = 'shorter_length', '1' = 
'longer_than_longer_than_shorter_length', '2' = 
'longer_than_shorter_length');

and I would like to get... whether the keys change or not..

$arrayToSort[0] == 'longer_than_longer_than_shorter_length';
$arrayToSort[1] == 'longer_than_shorter_length';
$arrayToSort[2] == 'shorter_length';
any ideas?

Rolf Brusletto
www.phpExamples.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Sorting an array by value length.

2003-10-22 Thread David Otton
On Wed, 22 Oct 2003 11:21:13 -0700, you wrote:

I've been scouring the php.net site for a few hours now trying to find a 
viable way to sort a single dimensional array( array('0'='fddfsdsfdds', 
'1'=','d','2' = 'gofofle'); ). so that the longest lengthed 
item is at the top, and the smallest lengthed item is at the bottom 
is there a function to do this... example:

?php

function cmp ($a, $b) {
$a = strlen ($a);
$b = strlen ($b);

if ($a == $b) return (0);
return (($a  $b) ? -1 : 1);
}

$a = array ('a', 'aa', 'aaa', '', 'a');

usort ($a, cmp);

print_r ($a);

?

Standard warning: usort() has side-effects:

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sorting multi-array

2003-07-24 Thread Ji Nmec
hello,

i have got a problem, tehere is an array:

$x = array(
   array(15,55,array(1,2,3),3,5,array(1,2,5)),
   array(25,55,array(1,2,3),3,5,array(1,2,5)),
   array(5,55,array(1,2,3),3,5,array(1,2,5))
   );

and I need to sort this arraybz first item of sub-arrays (x[0][0],
$x[1][0], $x[2][0]). this is the correct result:

$x = array(
   array(5,55,array(1,2,3),3,5,array(1,2,5)),
   array(15,55,array(1,2,3),3,5,array(1,2,5)),
   array(25,55,array(1,2,3),3,5,array(1,2,5))
   );

some idea how sort this multidimensional array? thank you for your
reply.

ji nmec, ICQ: 114651500
www.menea.cz - www strnky a aplikace


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting multi-array

2003-07-24 Thread Marek Kilimajer
www.php.net/usort - slight modification of the example (hint: add [0] to 
$a and $b).

Ji Nmec wrote:

hello,

i have got a problem, tehere is an array:

$x = array(
   array(15,55,array(1,2,3),3,5,array(1,2,5)),
   array(25,55,array(1,2,3),3,5,array(1,2,5)),
   array(5,55,array(1,2,3),3,5,array(1,2,5))
   );
and I need to sort this arraybz first item of sub-arrays (x[0][0],
$x[1][0], $x[2][0]). this is the correct result:
$x = array(
   array(5,55,array(1,2,3),3,5,array(1,2,5)),
   array(15,55,array(1,2,3),3,5,array(1,2,5)),
   array(25,55,array(1,2,3),3,5,array(1,2,5))
   );
some idea how sort this multidimensional array? thank you for your
reply.
ji nmec, ICQ: 114651500
www.menea.cz - www strnky a aplikace



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] sorting an array

2003-06-17 Thread Diana Castillo
Hi , I am trying to sort this:
array[numrooms] = Array ( [3] = 2 [2] = 5 [1] = 1 )
I want to get a result like this:
2=5
3=2
1=1
( I want the keys to stay attached to the results)

I used this to sort it :
 array_multisort ($request_array['numrooms'], SORT_NUMERIC, SORT_DESC);

but instead I get this:
 [numrooms] = Array ( [0] = 5 [1] = 2 [2] = 1 )

any suggestionis?



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array

2003-06-17 Thread Mark
perhaps asort()?
This function sorts an array such that array indices maintain their
correlation with the array elements they are associated with.


--- Diana Castillo [EMAIL PROTECTED] wrote:
 Hi , I am trying to sort this:
 array[numrooms] = Array ( [3] = 2 [2] = 5 [1] = 1 )
 I want to get a result like this:
 2=5
 3=2
 1=1
 ( I want the keys to stay attached to the results)
 
 I used this to sort it :
  array_multisort ($request_array['numrooms'], SORT_NUMERIC,
 SORT_DESC);
 
 but instead I get this:
  [numrooms] = Array ( [0] = 5 [1] = 2 [2] = 1 )
 
 any suggestionis?
 
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 


=
Mark Weinstock
[EMAIL PROTECTED]
***
You can't demand something as a right unless you are willing to fight to death to 
defend everyone else's right to the same thing.
***

__
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] sorting an array

2003-06-17 Thread CPT John W. Holmes
  array[numrooms] = Array ( [3] = 2 [2] = 5 [1] = 1 )
  I want to get a result like this:
  2=5
  3=2
  1=1
  ( I want the keys to stay attached to the results)
 
 perhaps asort()?
 This function sorts an array such that array indices maintain their
 correlation with the array elements they are associated with.

Or arsort(), which will sort them in decending order.

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] sorting an array

2003-03-20 Thread Steve Buehler
I am having problems sorting a multi-dimensional array.  I am hoping that 
somebody can help me figure out what I am doing wrong.  It should sort on 
the field than on the fac.  Here is my code:
-start code-
$sortterm;
get_locations();

function cmp ($a, $b) {
GLOBAL $sortterm;
return strcmp($a[$sortterm], $b[$sortterm]);
}
function get_locations(){
GLOBAL $sortterm;
$i=1;
while(($row=mysql_fetch_object($result))){
$locs[$i][loc_id]=$row-loc_id;
$locs[$i][fac]=$row-fac;
$locs[$i++][field]=$row-field;
}
	reset($locs);
	$sortterm=field;
	usort($locs, cmp);
	reset($locs);
	$sortterm=fac;
	usort($locs, cmp);
	reset($locs);
	while (list ($key, $value) = each ($locs)) {
		echo $locs[$key][loc_id]., .$locs[$key][fac]., 
.$locs[$key][field].br\n;
	}
}
-end code-

Here is how it comes out when run:
12, Johnson County Community College, C
11, Johnson County Community College, B
13, Johnson County Community College, D
14, Johnson County Community College, E
10, Johnson County Community College, A
8, Johnson County Girls Athletic Assoc, D
6, Johnson County Girls Athletic Assoc, B
7, Johnson County Girls Athletic Assoc, C
5, Johnson County Girls Athletic Assoc, A
9, Shawnee Civic Center, 1
1, Tomahawk Sports Dome, 1
2, Tomahawk Sports Dome, 2
3, Tomahawk Sports Dome, 3
4, Tomahawk Sports Dome, 4
As you can see, the fac is sorted correctly, but the field isn't.

Thanks
Steve


--
This message has been scanned for viruses and
dangerous content by the MailScanner at ow5, and is
believed to be clean.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] Sorting an array.

2002-04-18 Thread Mike Mike

Hello,
Here's what i'm working with.
I'm figuring the distance between to points based on
latitude and longitude.

while ($row = mysql_fetch_array($result))
{
//calculate distance between the two cities.
$Lat2 = $row[Latitude]; 
$Lon2 = $row[Longitude]; 
$x = 69.1 * ($Lat2 - $Lat1);
$y = 53 * ($Lon2 - $Lon1);

$Distance = sqrt($x * $x + $y * $y);
$Distance = ceil($Distance);

   if ($Distance  100){
echo $row[CompanyName].br;
echo $row[Address].br;
echo $row[City].br;
echo $row[State].br;
echo $row[ZipCode].br;
echo $row[PhoneNumber].br;
echo $row[FaxNumber].br;

echo Approximant Distance = $Distance
Milesbrbr;
}
}
Here is where my question comes to play.  How do I
sort the $Distance so that 4 Miles will come up first
10 second 30 third ext..
I have tried useing sort() function but it doesn't
work quite the way I want it to.
Thanks for your help
 --Mike

__
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Sorting an array.

2002-04-18 Thread Richard Emery

Perhaps you could store $row in an array of arrays; store only $row within
100 miles. Add another field, Distance, to each row that holds the
calculated distance.  Then, after creating the array, sort the final array
by Distance.
- Original Message -
From: Mike Mike [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Thursday, April 18, 2002 8:51 AM
Subject: [PHP] Sorting an array.


Hello,
Here's what i'm working with.
I'm figuring the distance between to points based on
latitude and longitude.

while ($row = mysql_fetch_array($result))
{
file://calculate distance between the two cities.
$Lat2 = $row[Latitude];
$Lon2 = $row[Longitude];
$x = 69.1 * ($Lat2 - $Lat1);
$y = 53 * ($Lon2 - $Lon1);

$Distance = sqrt($x * $x + $y * $y);
$Distance = ceil($Distance);

   if ($Distance  100){
echo $row[CompanyName].br;
echo $row[Address].br;
echo $row[City].br;
echo $row[State].br;
echo $row[ZipCode].br;
echo $row[PhoneNumber].br;
echo $row[FaxNumber].br;

echo Approximant Distance = $Distance
Milesbrbr;
}
}
Here is where my question comes to play.  How do I
sort the $Distance so that 4 Miles will come up first
10 second 30 third ext..
I have tried useing sort() function but it doesn't
work quite the way I want it to.
Thanks for your help
 --Mike

__
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/

--
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] Sorting an array.

2002-04-18 Thread Miguel Cruz

Sounds like it'd be a lot easier to let MySQL do the calculation and 
sorting.

  $userLatitude = 50;
  $userLongitude = 70;
  $sql = select companyname, blah, blah, 
 ceiling(sqrt(pow(69.1 * (latitude - $userLatitude), 2) +
 pow(53 * (longitude - $userLongitude), 2))) as distance
 from mytable order by distance;

miguel

On Thu, 18 Apr 2002, Mike Mike wrote:
 Here's what i'm working with.
 I'm figuring the distance between to points based on
 latitude and longitude.
 
 while ($row = mysql_fetch_array($result))
 {
   //calculate distance between the two cities.
   $Lat2 = $row[Latitude]; 
   $Lon2 = $row[Longitude]; 
   $x = 69.1 * ($Lat2 - $Lat1);
   $y = 53 * ($Lon2 - $Lon1);
   
   $Distance = sqrt($x * $x + $y * $y);
   $Distance = ceil($Distance);
 
if ($Distance  100){
   echo $row[CompanyName].br;
   echo $row[Address].br;
   echo $row[City].br;
   echo $row[State].br;
   echo $row[ZipCode].br;
   echo $row[PhoneNumber].br;
   echo $row[FaxNumber].br;
   
   echo Approximant Distance = $Distance
 Milesbrbr;
 }
 }
 Here is where my question comes to play.  How do I
 sort the $Distance so that 4 Miles will come up first
 10 second 30 third ext..
 I have tried useing sort() function but it doesn't
 work quite the way I want it to.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Sorting an array of arrays....

2002-02-14 Thread Fifield, Mike

What I am trying to do is sort a array of arrays but I want to sort by one
of the pieces of data stored in the arrays inside the array. For example;
$data[blue] = array(name, age, time, 3);
$data[green] = array(name, age, time, 7);
$data[red] = array(name, age, time, 6);
$data[yellow] = array(name, age, time, 2);
$data[white] = array(name, age, time, 1);
$data[black] = array(name, age, time, 9);

I want to be able to sort this array of arrays by the very last element, the
numbers. 

Mike Fifield
Charles Schwab  Co, Inc.
WARNING: All e-mail sent to or from this address will be received by the
Charles Schwab corporate e-mail system and is subject to archival and review
by someone other than the recipient.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Sorting an array of arrays....

2002-02-14 Thread Darren Gamble

Good day,

uasort() should do what you need.

http://www.php.net/manual/en/function.uasort.php

There's an example for the usort function that does something similar to
what you want, in fact.


Darren Gamble
Planner, Regional Services
Shaw Cablesystems GP
630 - 3rd Avenue SW
Calgary, Alberta, Canada
T2P 4L4
(403) 781-4948


-Original Message-
From: Fifield, Mike [mailto:[EMAIL PROTECTED]]
Sent: Thursday, February 14, 2002 9:26 AM
To: [EMAIL PROTECTED]
Subject: [PHP] Sorting an array of arrays


What I am trying to do is sort a array of arrays but I want to sort by one
of the pieces of data stored in the arrays inside the array. For example;
$data[blue] = array(name, age, time, 3);
$data[green] = array(name, age, time, 7);
$data[red] = array(name, age, time, 6);
$data[yellow] = array(name, age, time, 2);
$data[white] = array(name, age, time, 1);
$data[black] = array(name, age, time, 9);

I want to be able to sort this array of arrays by the very last element, the
numbers. 

Mike Fifield
Charles Schwab  Co, Inc.
WARNING: All e-mail sent to or from this address will be received by the
Charles Schwab corporate e-mail system and is subject to archival and review
by someone other than the recipient.



-- 
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] Sorting an array of arrays....

2002-02-14 Thread Fifield, Mike

Does anyone have an example of code that does this, I have been trying to
decipher the instructions on the use of uasort but it does not make a hole
lot of sense. They are using static keys and sorting by one of the array
elements of each of there static keys. It does not seem like one should have
to do it this way. I need it to go through each key and sort the keys based
on a value in the array held in that key. 

Thanks 

Mike

-Original Message-
From: Darren Gamble [mailto:[EMAIL PROTECTED]]
Sent: Thursday, February 14, 2002 9:35 AM
To: 'Fifield, Mike'; [EMAIL PROTECTED]
Subject: RE: [PHP] Sorting an array of arrays


Good day,

uasort() should do what you need.

http://www.php.net/manual/en/function.uasort.php

There's an example for the usort function that does something similar to
what you want, in fact.


Darren Gamble
Planner, Regional Services
Shaw Cablesystems GP
630 - 3rd Avenue SW
Calgary, Alberta, Canada
T2P 4L4
(403) 781-4948


-Original Message-
From: Fifield, Mike [mailto:[EMAIL PROTECTED]]
Sent: Thursday, February 14, 2002 9:26 AM
To: [EMAIL PROTECTED]
Subject: [PHP] Sorting an array of arrays


What I am trying to do is sort a array of arrays but I want to sort by one
of the pieces of data stored in the arrays inside the array. For example;
$data[blue] = array(name, age, time, 3);
$data[green] = array(name, age, time, 7);
$data[red] = array(name, age, time, 6);
$data[yellow] = array(name, age, time, 2);
$data[white] = array(name, age, time, 1);
$data[black] = array(name, age, time, 9);

I want to be able to sort this array of arrays by the very last element, the
numbers. 

Mike Fifield
Charles Schwab  Co, Inc.
WARNING: All e-mail sent to or from this address will be received by the
Charles Schwab corporate e-mail system and is subject to archival and review
by someone other than the recipient.



-- 
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] Sorting an array of arrays....

2002-02-14 Thread Lars Torben Wilson

On Thu, 2002-02-14 at 08:26, Fifield, Mike wrote:
 What I am trying to do is sort a array of arrays but I want to sort by one
 of the pieces of data stored in the arrays inside the array. For example;
 $data[blue] = array(name, age, time, 3);
 $data[green] = array(name, age, time, 7);
 $data[red] = array(name, age, time, 6);
 $data[yellow] = array(name, age, time, 2);
 $data[white] = array(name, age, time, 1);
 $data[black] = array(name, age, time, 9);
 
 I want to be able to sort this array of arrays by the very last element, the
 numbers. 

?php
error_reporting(E_ALL);

$data = array();
$data['blue'] = array(name, age, time, 3);
$data['green'] = array(name, age, time, 7);
$data['red'] = array(name, age, time, 6);
$data['yellow'] = array(name, age, time, 2);
$data['white'] = array(name, age, time, 1);
$data['black'] = array(name, age, time, 9);
$data['spooge'] = array();

function cmp($a, $b) {
/* Bounds checking. To put any empty arrays at the 
 * end instead of the beginning, swap -1 and 1 in the
 * next two lines. */
if (empty($a)  !empty($b)) return -1;
if (empty($b)  !empty($a)) return 1;
if (empty($a)  empty($b)) return 0;
return (int) ($a[count($a) - 1] - $b[count($b) - 1]);
}

uasort($data, 'cmp');

print_r($data);
?


Hope this helps,

Torben

 Mike Fifield
 Charles Schwab  Co, Inc.
 WARNING: All e-mail sent to or from this address will be received by the
 Charles Schwab corporate e-mail system and is subject to archival and review
 by someone other than the recipient.

-- 
 Torben Wilson [EMAIL PROTECTED]
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Sorting an array of arrays....

2002-02-14 Thread Toni Kustiana


try to add to your script:
 
function cmp ($a, $b) {
return strcmp($a[3],$b[3]);
}
usort($data, cmp);
  Fifield, Mike [EMAIL PROTECTED] wrote: What I am trying to do is sort a 
array of arrays but I want to sort by one
of the pieces of data stored in the arrays inside the array. For example;
$data[blue] = array(name, age, time, 3);
$data[green] = array(name, age, time, 7);
$data[red] = array(name, age, time, 6);
$data[yellow] = array(name, age, time, 2);
$data[white] = array(name, age, time, 1);
$data[black] = array(name, age, time, 9);

I want to be able to sort this array of arrays by the very last element, the
numbers. 

Mike Fifield
Charles Schwab  Co, Inc.
WARNING: All e-mail sent to or from this address will be received by the
Charles Schwab corporate e-mail system and is subject to archival and review
by someone other than the recipient.



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



-
Do You Yahoo!?
Send FREE Valentine eCards with Yahoo! Greetings!


[PHP] Sorting Multidimensional Array..second dimension has 12 elements

2001-12-08 Thread J. Roberts

I have a 2 dimensional array...with the second dimension having 
12 elements.  What I want to do is sort (and re-sort) they array
by one of the 12 elements...keeping data consistent.

All of the examples I have read @php.net seem to deal with 
multidim-arrays that only have one element in them.

Here is a cut-n-paste of a print_r() of my array:
-begin paste--
Array
(
[barnhij] = Array
(
[name] = Jeremy Barnhill
[toi] = 5855
[pdk] = 1456
[tpdk_trans] = 40169
[tpallm_trans] = 17430
[terrors] = 174
[total_trans] = 57599
[pct] = 97.03
[modpct] = 98.3630
[pdk_pct] = 69.74
[pallm_pct] = 30.26
[trans_per_req] = 9.84
)

[clarkm10] = Array
(
[name] = Michelle Clark
[toi] = 1971
[pdk] = 402
[tpdk_trans] = 15427
[tpallm_trans] = 9970
[terrors] = 97
[total_trans] = 25397
[pct] = 95.08
[modpct] = 97.3484
[pdk_pct] = 60.74
[pallm_pct] = 39.26
[trans_per_req] = 12.89
)

[humphrj5] = Array
(
[name] = Jill Humphries
[toi] = 6104
[pdk] = 1002
[tpdk_trans] = 42759
[tpallm_trans] = 28907
[terrors] = 125
[total_trans] = 71666
[pct] = 97.95
[modpct] = 98.8889
[pdk_pct] = 59.66
[pallm_pct] = 40.34
[trans_per_req] = 11.74
)

[roberj15] = Array
(
[name] = Jamison Roberts
[toi] = 7132
[pdk] = 1524
[tpdk_trans] = 52542
[tpallm_trans] = 42311
[terrors] = 46
[total_trans] = 94853
[pct] = 99.36
[modpct] = 99.6533
[pdk_pct] = 55.39
[pallm_pct] = 44.61
[trans_per_req] = 13.30
)

)

---end paste-

Hope this is clear,
Thanks - Jamison.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] sorting an array of objects

2001-10-24 Thread L Melville

Hi,

Is it possible to sort a class array using array multisort, I have two items
in each class that I wish to sort by, one is a text and the other is the
first item in an array within within each class. i.e.;-

$class[$index]-text;
$class[$index]-array[0];

how can I do this?

thanks in advance
lee




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] sorting an array of objects

2001-10-24 Thread Matt McClanahan

On Wed, Oct 24, 2001 at 10:43:53AM +0100, L Melville wrote:

 Is it possible to sort a class array using array multisort, I have two items
 in each class that I wish to sort by, one is a text and the other is the
 first item in an array within within each class. i.e.;-
 
 $class[$index]-text;
 $class[$index]-array[0];
 
 how can I do this?

I'd use usort() instead.  http://www.php.net/manual/en/function.usort.php

The manual page has a couple examples for sorting objects by their member
variables.

Matt

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Sorting an array

2001-09-13 Thread Dotan Cohen

I'm trying to sort a listing of albums alphabetically
for LyricsList.com, the problem is that I want the
initial 'The' and 'A' in some albums (A Hard Days
Night, The Wall) to be ignored. So that the list will
look like: 

Abbey Road 
Dark Side Of the Moon 
The Final Cut 
A Hard Days Night 
Kill 'em All 
The Wall 

ABC order, 'A' and 'The' negated. The info comes from
a database, in three calls: 

// call albums that begin with letter $letter 
$query = SELECT * FROM albums WHERE album LIKE
'$letter%' ORDER BY album; 
$result = mysql_query($query) or die(mysql_error()); 

// call albums that begin with letter A 
$query_a = SELECT * FROM albums WHERE album LIKE 'a'
ORDER BY album; 
$result_a = mysql_query($query_a) or
die(mysql_error()); 

// call albums that begin with letter T 
$query_t = SELECT * FROM albums WHERE album LIKE 't'
ORDER BY album; 
$result_t = mysql_query($query_t) or
die(mysql_error()); 

So I need to get just the albums from $result_t that
begin with 'The ' (easy regex) and from $result_a that
begin with 'A ' (easy regex), but puting the three of
them in order is proving beyond my skill. 

Thanks in advance for any advise. 

Dotan Cohen


__
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Sorting an array

2001-01-18 Thread Nicklas af Ekenstam

Hi!

I have an array that looks kinda like this:

NAMEADDRESS
John DoeSome Street 1
Jane DoeAnother Street 2
Bill Gates  Helsinki 666

How do I sort this array based on NAME? ADDRESS?

Thanks!!

- Nicklas


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Sorting an array

2001-01-18 Thread Pavel Jartsev

Nicklas af Ekenstam wrote:
 
 Hi!
 
 I have an array that looks kinda like this:
 
 NAMEADDRESS
 John DoeSome Street 1
 Jane DoeAnother Street 2
 Bill Gates  Helsinki 666
 
 How do I sort this array based on NAME? ADDRESS?

http://www.php.net/manual/en/function.usort.php


-- 
Pavel a.k.a. Papi

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]