Jump to content

order my random array


canute1

Recommended Posts

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

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

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.