note 43499 deleted from function.asort by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 06:46:46 +0000 |
| Subject: | note 43499 deleted from function.asort by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196634@lists.php.net to get a copy of this message | ||
Note Submitter: csaba at alum dot mit dot edu
----
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:
<?php
$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