我检查了所有已被问过的问题。我找不到合适的人。问题是对数组进行排序和rsort,但是不允许使用sort和rsort unfunction。它应该用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";
?>
答案 0 :(得分:0)
我能想象的最简单的方法是使用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()。
问候。
答案 1 :(得分:0)
这是我很久以前学到的泡泡分类程序。可能更简单的方法,但它的工作原理。我已经包含了打印件,因此当阵列发生变化时,您可以看到正在发生的事情。
<?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));
?>