我检查了所有已经提出的问题。我没有找到合适的。问题是对数组进行排序和rsort,但不允许使用sort和rsort非函数。它应该用for循环来完成。
<?php
$numberstring = $_GET['numberstring'];
$array = explode(',',$numberstring);
echo "Order in the beginning: $numberstring'n";
// My code starts from here for the missing part
for ($i=0;$i<count($array);$i++)
{
if ($get_largest<$array[$i])
{$get_largest=$array[$i];
$get_largest=range(0,count($array));//I 'm not sure of this line
}
if ($get_smallest>$array[$i])
{
$get_smallest=$array[$i];
$get_smallest=range(0,count($array));
}
}
$largest_smallest=explode(',',$get_largest);
$smallest_largest=explode(' ,',$get_smallest);
// my code finished
echo "Largest to smallest: $largest_smallest'n";
echo "Smallest to largest: $smallest_largest'n";
?>
我能想到的最简单的方法是使用min()和max()函数。
下面是一个例子:
<?
function array_sort($input,$reverse){
unset($new_array);
$new_array = array(); // This will be our output where sorted values coming in //
if($reverse == false){ // RSort or not //
for($i=0;$i<count($input);$i){ // loop as many times as many values are stored in the array //
$get_smallest = min($input); // get the smallest value of the input array //
$key = array_search($get_smallest, $input); // get the index of the smallest array to unset it later //
$new_array[] = $get_smallest; // store the smallest value in a new array //
unset($input[$key]); // unset (delete) the extracted (smallest) value so the min()-function grabs the next one in the next loop //
}
}
else{ // RSort or not //
for($i=0;$i<count($input);$i){
$get_biggest = max($input);
$key = array_search($get_biggest, $input);
$new_array[] = $get_biggest;
unset($input[$key]);
}
}
return $new_array;
}
$unsorted_array = array( 'ab','aa','ac','bf','be'); // Our test array
$output1 = array_sort($unsorted_array,false);
$output2 = array_sort($unsorted_array,true);
var_dump($output1);
echo '<br><br>';
var_dump($output2);
?>
输出:1
array(5) {
[0]=> string(2) "aa"
[1]=> string(2) "ab"
[2]=> string(2) "ac"
[3]=> string(2) "be"
[4]=> string(2) "bf"
}
输出:2
array(5) {
[0]=> string(2) "bf"
[1]=> string(2) "be"
[2]=> string(2) "ac"
[3]=> string(2) "ab"
[4]=> string(2) "aa"
}
因此,使用此函数,您可以在正常和反向模式下对任何数组进行排序。我还没有评论反向块。它与上面的块相同,但使用了max()而不是min()。
问候。
这是我很久以前学过的冒泡排序程序。也许更简单的方法,但它是有效的。我已经包括了打印,这样你就可以看到阵列变化时发生了什么。
<?php
$numbers = '2,24,21,2,3,77,900,1,4,5';
$array = explode(',',$numbers);
function bubblesort($numbers){
$array['mintomax'] = $numbers;
$array['maxtomin'] = $numbers;
while(true){
$shift_detected = false;
for($i=0;$i<count($numbers)-1;$i++){
$next_Var = $i+1;
if($array['mintomax'][$i]>$array['mintomax'][$next_Var]){
echo $i.': '.$next_Var.':';
$hold_var = $array['mintomax'][$i];
$array['mintomax'][$i] = $array['mintomax'][$next_Var];
$array['mintomax'][$next_Var] = $hold_var;
$shift_detected = true;
print_r($array['mintomax']);
echo '<br />';
}
}
if(!$shift_detected){
echo '<br /><br />';
break;
}
}
while(true){
$shift_detected = false;
for($i=0;$i<count($numbers);$i++){
$next_Var = $i+1;
if($array['maxtomin'][$i]<$array['maxtomin'][$next_Var]){
echo $i.': '.$next_Var.':';
$hold_var = $array['maxtomin'][$i];
$array['maxtomin'][$i] = $array['maxtomin'][$next_Var];
$array['maxtomin'][$next_Var] = $hold_var;
$shift_detected = true;
print_r($array['maxtomin']);
echo '<br />';
}
}
if(!$shift_detected){
echo '<br /><br />';
break;
}
}
return $array;
}
print_r(bubblesort($array));
?>