所以这可能是一些简单的事情,我只是在看,但我需要弄清楚如何做到这一点。这是我正在上的一门课的问题,但我不指望有人为我做我的工作(我永远不会那样学习)我更多的是寻找一个好的起点。在此示例中,我需要创建一个数组,对其进行排序,然后对其进行反转。我得到了那么多。
最后一部分是将给定数组与可能反转的版本进行比较,如果反转正确则输出“True”,否则输出“False”。它还需要在不使用内置 php 数组函数的情况下完成。我还是编程新手,想学习。任何帮助将不胜感激,因为我什至不知道从最后一部分开始。这就是我到目前为止所拥有的。
<?php
//Original array
$the_array = array(5,9,2,8,3,1,7,6,4);
$arrayString = implode(',',$the_array);
echo ("The original array is: ");
echo $arrayString;
echo "<br />";
//Sort loop
// Compares each value in the array to the next one by seeing if the value is greater then the next +1
// If the value is greater then the next value +1 then it stores the value and moves it into the next position.
for ($j = 0; $j < count($the_array); $j++)
{
for ($i = 0; $i < count($the_array)-1; $i++)
{
if ($the_array[$i] > $the_array[$i+1])
{
$tmp = $the_array[$i+1];
$the_array[$i+1] = $the_array[$i];
$the_array[$i] = $tmp;
}
}
}
$arrayString = implode(',',$the_array);
echo ("The array after sorting is: ");
echo $arrayString;
echo "<br />";
//reversal loop
for ($tmp = sizeof($the_array) - 1; $tmp >= 0; $tmp--)
{
$reverse[] = $the_array[$tmp]; //New reversed array
}
echo ("The reversed array is: ");
$reverseString = implode(',',$reverse);
echo $reverseString;
我的代码格式中的任何提示也将不胜感激。
实际上,我想对我收到的所有回复以及获得回复的速度表示感谢。所有的评论和答案已经开始给我很大的帮助。我想我可能只是在思考这个问题,而向我指出的一些事情帮助我意识到了这一点。再次感谢大家。