note 43499 modified in function.asort by vrana

From: Date: Wed, 04 Aug 2004 17:20:39 +0000
Subject: note 43499 modified in function.asort by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-73939@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: <?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 --was-- 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 http://php.net/manual/en/function.asort.php

« previous php.notes (#73939) next »