So this may be something simple that I am just over looking, but I need to figure out how to do this. This is a question for a class I'm taking, but am not expecting someone to do my work for me ( I'll never learn that way ) I'm more looking for a good starting point. In this example I needed to create an array, sort it, then reverse it. I got that much down.
The final part is to compare the given array with the possibly reversed version and output “True” if it is a correct reversal and output “False” otherwise. It also needs to be done without using the built in php array functions. I'm still new to programming and want to learn. Any help would be much appreciated as I don't even know where to begin with this last part. This is what I have so far.
<?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;
Any tips in the formatting of my code would also be appreciated.
I actually want to give a thank you to all the responses I got and to the speed of getting them. All the comments and answers have already started to give me a lot of help. I think I may have just been over thinking the problem and some of the things pointed out to me helped me realize this. Thanks again to all.