Lt Llama Posted January 18, 2007 Share Posted January 18, 2007 I save some values in an array with:[code=php:0]$myArray[$i++] = array($value1,$value2);[/code]Lets say I have the following content of $myArray:$myArray[0]: $value1 = "1", $value2= "a"$myArray[1]: $value1 = "2", $value2= "b"$myArray[2]: $value1 = "3", $value2= "c"$myArray[3]: $value1 = "1", $value2= "d"How do I sort reverse on $value1 so I can use:[code=php:0]for ($j=0; $j <= $i; $j++) { echo $myArray[$j][0],"...",$myArray[$j][1],"<br>";}[/code]to print[tt]3 ... c2 ... b1 ... a1 ... d[/tt]I wish I could use something likesort($myArray[$value1]);Sorry, but I just don't get this.Isn't there a simple php function to sort an indexed array based on a value in the array?p.s. $value1 and $value2 contains non unique numbers. Link to comment https://forums.phpfreaks.com/topic/34765-sort-indexed-array-based-on-value-in-the-array/ Share on other sites More sharing options...
The Little Guy Posted January 18, 2007 Share Posted January 18, 2007 I'm thinking your looking to do this:http://us3.php.net/manual/en/function.arsort.php Link to comment https://forums.phpfreaks.com/topic/34765-sort-indexed-array-based-on-value-in-the-array/#findComment-163870 Share on other sites More sharing options...
Lt Llama Posted January 19, 2007 Author Share Posted January 19, 2007 [quote author=The Little Guy link=topic=123015.msg508001#msg508001 date=1169146077]I'm thinking your looking to do this:http://us3.php.net/manual/en/function.arsort.php[/quote]arsort seems to be good if you want to sort the elements in the array like$myArray = array("b","a")after arsort becomes$myArray = array("a","b").But thats not my problem.I want to sort like this:.This array content:$myArray[0] = array("1","a")$myArray[1] = array("1","a")$myArray[2] = array("2","c")after sort will be$myArray[2] = array("2","c")$myArray[0] = array("1","a")$myArray[1] = array("1","b")In other words:$myArray[index] = array($sortedValue,$notSortedValue)$myArray[index] = array($sortedValue,$notSortedValue)$myArray[index] = array($sortedValue,$notSortedValue) Link to comment https://forums.phpfreaks.com/topic/34765-sort-indexed-array-based-on-value-in-the-array/#findComment-164326 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.