[PHP] array sort help...

2003-06-11 Thread Dan Joseph
Hi,

I cannot figure this out...  Need some assistance.

I have an array:

$jack[#] = array(
loan_info = 101,
first_name = jack,
last_name = mother
);

# = 0 thru 12

I want to sort the array by loan_info.  Could someone please explain this
to me?  I kind of understand array sorting, but I am still lost...

-Dan Joseph


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



Re: [PHP] array sort help...

2003-06-11 Thread Lars Torben Wilson
On Wed, 2003-06-11 at 12:10, Dan Joseph wrote:
 Hi,
 
   I cannot figure this out...  Need some assistance.
 
   I have an array:
 
   $jack[#] = array(
   loan_info = 101,
   first_name = jack,
   last_name = mother
   );
 
   # = 0 thru 12
 
   I want to sort the array by loan_info.  Could someone please explain this
 to me?  I kind of understand array sorting, but I am still lost...
 
 -Dan Joseph

If you want to maintain key order, use uasort(); if, however, you want
the array to be reindexed sequentially when you sort it, use usort():

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

A short example (with the usual caveats about 'make sure you add error
checking):


?php
error_reporting(E_ALL);

$data = 
array(array('loan_info' = 104, 
'first_name' = 'jack', 
'last_name' = 'mother'),
  array('loan_info' = 102, 
'first_name' = 'jill', 
'last_name' = 'brother'),
  array('loan_info' = 108, 
'first_name' = 'jim', 
'last_name' = 'the'),
  array('loan_info' = 107, 
'first_name' = 'bob', 
'last_name' = 'whole'),
  array('loan_info' = 105, 
'first_name' = 'alice', 
'last_name' = 'fam'),
  array('loan_info' = 103, 
'first_name' = 'ellen', 
'last_name' = 'damily'));

function cmp($a, $b) {
return strcmp($a['loan_info'], $b['loan_info']);
}

uasort($data, 'cmp');

print_r($data);

?



Hope this helps,

Torben


-- 
 Torben Wilson [EMAIL PROTECTED]+1.604.709.0506
 http://www.thebuttlesschaps.com  http://www.inflatableeye.com
 http://www.hybrid17.com  http://www.themainonmain.com
 - Boycott Starbucks!  http://www.haidabuckscafe.com -




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