Jump to content

how to connect values and keys and then sort them?


tastro

Recommended Posts

$a=array('juice','milk','cola');
$a2=array('1','3','2');

for( $i = 0; $i < count($a); $i++ ) {
$new[$a2[$i]] = $a[$i];
}

ksort($new);
print_r($new); // returns Array ( [1] => juice [2] => cola [3] => milk )

ok this works fine now... but some of my numbers are the same also like this:

 

$a=array('juice','milk','cola','sprite');

$a2=array('1','3','2','3');

 

and then the second key with the same number doesn't get printed out. :S

 

if you fix this then all will work perfect. :>

 

thank you both for the one before, both worked.

also to explain it more... i have a ranking system on my site... from rank 1 to rank 5. and some users have the same rank, also there are more users with rank 2 and 3... and i have to sort theyr posts by theyr rank.

 

'juice','milk','cola','sprite' <--- this are the post titles

 

'1','3','2','3' <--- and this are the rankings of the users who made the posts

don't know how to do this.

 

but i found another way myself right now:

 

$ar1=array('1','3','2','3');
$ar2=array('juice','milk','cola','sprite');
array_multisort($ar1,$ar2);
print_r($ar2);

 

i didn't knew that this function even exists: array_multisort();

 

but would like to see your idea as well please (maybe it's faster or uses less resources). thank you. ;)

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.