tastro Posted January 26, 2011 Share Posted January 26, 2011 how to connect juice and 1 and milk and 3 and cola and 2? and then sort it by the numbers from high to low? $a=array('juice','milk','cola'); $a2=array('1','3','2'); Quote Link to comment Share on other sites More sharing options...
PinoyProgrammer Posted January 26, 2011 Share Posted January 26, 2011 $a1 = array ('juice', 'milk', 'cola'); $a2 = array ('1', '3', '2'); foreach ($a2 as $k => $v) $a3[$v] = $a1[$k]; sort ($a3); print ($a3); Quote Link to comment Share on other sites More sharing options...
tastro Posted January 26, 2011 Author Share Posted January 26, 2011 the order is not right. now i get: Array ( [0] => cola [1] => juice [2] => milk ) but it should order by $a2 numbers. Quote Link to comment Share on other sites More sharing options...
PinoyProgrammer Posted January 26, 2011 Share Posted January 26, 2011 apologies. replace sort() with ksort(). Edit: Refer to this manual http://www.php.net/manual/en/ref.array.php Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 26, 2011 Share Posted January 26, 2011 $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 ) Quote Link to comment Share on other sites More sharing options...
tastro Posted January 26, 2011 Author Share Posted January 26, 2011 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 26, 2011 Share Posted January 26, 2011 You can't have two array elements with the same index. Quote Link to comment Share on other sites More sharing options...
tastro Posted January 26, 2011 Author Share Posted January 26, 2011 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 26, 2011 Share Posted January 26, 2011 Where are these values coming from? Are they stored in a database then retrieved for display? Quote Link to comment Share on other sites More sharing options...
tastro Posted January 26, 2011 Author Share Posted January 26, 2011 yes... i get them as arrays from the database. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 26, 2011 Share Posted January 26, 2011 Why not group and sort them at that time instead of all this looping through arrays and changing indices, etc.? Quote Link to comment Share on other sites More sharing options...
tastro Posted January 26, 2011 Author Share Posted January 26, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.