所以這可能是一件簡單的事情,我只是在看,但我需要弄清楚如何做到這一點。這是我正在上課的一個問題,但我並不期望有人爲我做我的工作(我永遠不會這樣學習),我更希望找到一個好的起點。在這個例子中,我需要創建一個數組,然後對其進行排序。我得到了很多。比較兩個php數組,看看其中一個是否與另一個相反
最後一部分是將給定數組與可能的反轉版本進行比較,如果是正確的反轉則輸出「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;
我的代碼格式化的任何提示也將不勝感激。
我實際上想要感謝所有我得到的答覆以及獲得答案的速度。所有的評論和答案已經開始給我很多幫助。我想我可能剛剛結束思考這個問題,並且指出的一些事情幫助我意識到了這一點。再次感謝所有人。
爲什麼你不能只在給定的數組上運行你的反轉函數,然後比較它的第二個數組,看看它是否確實是反向的呢? –
@Prix:不需要知道數組是否已排序,問題應該集中在如何確定兩個數組是否相反。 –
@DaveChen這就是所謂的基礎編輯,它不被接受。 – Prix