Re: Sorting multi-dimensional arrays

From: Date: Tue, 07 Nov 2000 12:44:55 +0000
Subject: Re: Sorting multi-dimensional arrays
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-24087@lists.php.net to get a copy of this message
>>>>> "HM" == "Tobias H Michaelsen" <thm-dev@iname.com> >>>>> writes: > Okey, a more 'real-life'-like example of what i want: I have a > text-file, containing information about some friends; id#, name, > birthdate, telephone etc. > -person.txt- 0|:|John Doe|:|19640924|:|555-1354|:|... 1|:|Jane > Pho|:|19731030|:|555-5793|:|... 2|:|Mike > Gnu|:|19700227|:|555-3426|:|... -person.txt- > And I want to read the information into a multi-dimensional array > (ie. an array of arrays), and then be able to sort the information > according to a specific column -- like if the information had been > stored in a database, and retrieved with ex. "SELECT * FROM person > ORDER BY birthdate". This is something I wrote while playing with create_function() [PHP 4.0.1 or higher] which should do pretty much what you described. Sorry about the readability, I was experimenting... <?php function array_column_sort( &$array, $i = 0 ) { usort($array,create_function("\$a,\$b","return \$a[$i]<\$b[$i]?-1:\$a[$i]!=\$b[$i];")); } ?> -- Robin Vickery............................................... Planet-Three, 3A West Point, Warple Way, London, W3 0RG, UK Email: robin@planet-three.net Phone: +44 (0)870 729 5444

« previous php.general (#24087) next »