note 27583 modified in function.usort by danbrown

From: Date: Sun, 23 Jan 2011 16:31:07 +0000
Subject: note 27583 modified in function.usort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176081@lists.php.net to get a copy of this message
I like to define my arrays to look like tables in a database or spreadsheet as follows (it looks tidy is the only reason!): Row(s) Column(s) -------------> | $array[0] = array("1", "2","3"); | $array[1] = array("1", "2","3"); \/ $array[2] = array("1", "2","3"); This "array of arrays" seems to behave differently to normal associative or multi-dimension arrays when sorting but using franky's routine.... <?php function cmp ($a, $b) { global $w_o; if ($a[$w_o] == $b[$w_o]) return 0; return ($a[$w_o] < $b[$w_o]) ? -1 : 1; } ?> ...you simply specify the column you want to sort by defining $w_o and call "usort($my_array,"cmp");". This might seem obvious to some people but wasn't to me and I hope this helps others in the same situation. Thanks.... --was-- Thankyou to franky at iname dot com for his solution to my problem although i'd like to clarify something because the use for this isn't entirely obvious at first glance. I like to define my arrays to look like tables in a database or spreadsheet as follows (it looks tidy is the only reason!): Row(s) Column(s) -------------> | $array[0] = array("1", "2","3"); | $array[1] = array("1", "2","3"); \/ $array[2] = array("1", "2","3"); This "array of arrays" seems to behave differently to normal associative or multi-dimension arrays when sorting but using franky's routine.... <?php function cmp ($a, $b) { global $w_o; if ($a[$w_o] == $b[$w_o]) return 0; return ($a[$w_o] < $b[$w_o]) ? -1 : 1; } ?> ...you simply specify the column you want to sort by defining $w_o and call "usort($my_array,"cmp");". This might seem obvious to some people but wasn't to me and I hope this helps others in the same situation. Thanks.... http://php.net/manual/en/function.usort.php

« previous php.notes (#176081) next »