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'); Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/ 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); Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165325 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. Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165328 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 Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165331 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 ) Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165332 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. Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165336 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. Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165338 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 Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165340 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? Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165341 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. Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165342 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.? Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165346 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. Link to comment https://forums.phpfreaks.com/topic/225696-how-to-connect-values-and-keys-and-then-sort-them/#findComment-1165347 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.