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.
Link to comment
Share on other sites

[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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.