note 43499 added to function.asort
| From: | csaba at alum dot mit dot edu | Date: | Wed, 23 Jun 2004 08:47:15 +0000 |
| Subject: | note 43499 added to function.asort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-71537@lists.php.net to get a copy of this message | ||
If you have a pair of arrays which have a one to one association (examples: spouses, first to last
name, SSN to name), when you sort one, you might wish to sort the other in the same way to maintain
the correlation. This example illustrates a way:
$aMen = array('Fred', 'Bob', 'Tim', 'John',
'Bill');
$aPartner = array('Sue', 'Mary', 'Ann', 'Cathy',
'Nancy');
asort($aMen); // aMen now sorted; numeric keys out of order
$aWomen = array_keys($aMen); // create a new array for result
foreach ($aWomen as $idx => &$name) $name=$aPartner[$name];
// aWomen now has the sorted partners
$aMen = array_merge($aMen); // put the numeric keys in order
Csaba Gabor
----
Manual Page -- http://www.php.net/manual/en/function.asort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43499
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43499&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43499&report=yes
Search -- http://master.php.net/manage/user-notes.php