Jump to content

Sort indexed array based on value in the array


Lt Llama

Recommended Posts

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 ... c
2 ... b
1 ... a
1 ... d
[/tt]

I wish I could use something like
sort($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.
[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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.