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; } Link to comment https://forums.phpfreaks.com/topic/60234-order-my-random-array/ Share on other sites More sharing options...
trq Posted July 16, 2007 Share Posted July 16, 2007 Pardon? Link to comment https://forums.phpfreaks.com/topic/60234-order-my-random-array/#findComment-299638 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; } ?> Link to comment https://forums.phpfreaks.com/topic/60234-order-my-random-array/#findComment-299649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.