我是php的新手,我正在做这个分配,我只应该使用for循环做整个代码的缺失代码。有谁能告诉我我错过了什么吗?提前感谢!希望我的for循环工作正常,我得到的输出是:
Order in the beginning: 4,7,-2,0,6
Largest to smallest: Array
Smallest to largest: Array
应该是例如:
Order in the beginning: 4,7,-2,0,6
Largest to smallest: 7,6,4,0,-2
Smallest to largest: -2,0,4,6,7
任务:
完成以下PHP脚本,以便它以特定顺序打印表单中给定的数字。脚本应该组织从最大到最小和从最小到最大的数字,并在屏幕上打印这两个数字字符串。这些点以字符串的形式发送给脚本,其中的点用逗号分隔(例如4,5,2)。使用explosion -function将点划分为一个数组。不允许使用sort-函数。用for语句来组织。不完整的项目:
<?php
$numberstring = $_GET['numberstring'];
$array = explode(',',$numberstring);
echo "Order in the beginning: $numberstring'n";
// Your code here and only here
echo "Largest to smallest: $largest_smallest'n";
echo "Smallest to largest: $smallest_largest'n";
?>
按降序和降序排列输入的代码:
for ($j = 0; $j < count($array); $j++) {
for ($i = 0; $i < count($array) - 1; $i++) {
if ($array[$i] > $array[$i + 1]) {
$temp = $array[$i + 1];
$array[$i + 1] = $array[$i];
$array[$i] = $temp;
}
}
$largest_smallest=$array;
}
for ($j = 0; $j < count($array); $j++) {
for ($i = 0; $i < count($array) - 1; $i++) {
if ($array[$i] < $array[$i + 1]) {
$temp = $array[$i + 1];
$array[$i + 1] = $array[$i];
$array[$i] = $temp;
}
}
$smallest_largest=$array;
}
基本上,您看到Array
是因为您不能像打印字符串那样打印数组。如果希望输出的形式与输入相同,用逗号分隔,则需要使用函数来实现。如果explode
通过分隔符将字符串分隔成数组,您能想到一个相反的函数名称吗?