canute1 Posted July 16, 2007 Share Posted July 16, 2007 Dear Group Can anybody help me select a specific order (not a sort but a particular order) rather than this random order that is generated (and returned by $new_arr) in the code below?! Thanks in advance!: $max = 6; $index = 0; $new_arr = array(); while (count($array) > 0 && count($new_arr) <= $max) { $val = array_rand($array); $new_arr[$index] = $array[$val]; unset($array[$val]); $index++; } unset($max, $array, $index, $val); return $new_arr; } Quote Link to comment Share on other sites More sharing options...
trq Posted July 16, 2007 Share Posted July 16, 2007 Pardon? Quote Link to comment Share on other sites More sharing options...
lococobra Posted July 16, 2007 Share Posted July 16, 2007 If you want to put stuff in a specific order, different various sorts are the way to go... Just depends how you want to order them. <?php //Sort the normal way... sort($your_array); //Sort as a human would natsort($your_array); //Or my favorite.. sort by length of string $your_array = lensort($your_array); function sortlen($input){ $alens = $output = array(); foreach($input as $i => $item) $alens[$i] = strlen($item); natsort($alens); foreach(array_keys($alens) as $i) $output[] = $input[$i]; return $output; } ?> 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.