0

Hi I have some homework that I need to get done here. Our instructions are to manipulate an array and add, replace, sort, and change an array as asked. I have gotten pretty much everything done up the the point where my teacher wants me to ADD a Finch to the beginning of an array. I know how to get it at the end without a problem. I even know how to get it at the beginning by replacing the previous value with Finch. But is there a way to get the Finch up at the top of the Array without replacing the previous value in the 0 index position? Here are the instructions I have to follow, here is all of the code I have done so far:

Sort the array then display it's contents.
Reverse the array then display it's contents.
Remove the first element (Scarlet Tanager) from the array then display it's contents.
Add "Robin" and "Sandpiper" to the end of the array then display it's contents.
Replace "Chickadee" with "Coot" then display it's contents.
Add "Finch" to the beginning of the array then display it's contents.
Create another array of consisting of three birds (your choice). Name the array $newBirds then display it's contents.

<?php    
 $strBirds = "Whip-poor-will | Chickadee | Pileated Woodpecker | Blue Jay | Rufus-sided Towhee | Scarlet Tanager";
 $birdArray = explode("|",$strBirds);
 sort($birdArray);
 print "<h1>Sorted Birds</h1>";
 print "<ul>";
 FOREACH($birdArray as $birds)
 {
     print "<li>$birds</li>";
 }
 print "</ul>";
 
 
 print "<h1>Reverse Sorted Birds</h1>";
 rsort($birdArray);
 print "<ul>";
 FOREACH($birdArray as $birds)
 {
     print "<li>$birds</li>";
 }
 print "</ul>";
 
 print "<h1>Reverse Sorted Birds Without First Bird</h1>";
 unset($birdArray[1]);
 rsort($birdArray);
 print "<ul>";
 FOREACH($birdArray as $birds)
 {
     print "<li>$birds</li>";
 }
 print "</ul>";
 
 print "<h1>Two birds Added</h1>";
 $birdArray[] = "Robin";
 $birdArray[] = "Sandpiper";
 print "<ul>";
 FOREACH($birdArray as $birds)
 {
     print "<li>$birds</li>";
 }
 print "</ul>";
 
 print "<h1>Chickadee Replaced with Coot</h1>";
 $birdArray[3] = "Coot";
 print "<ul>";
 FOREACH($birdArray as $birds)
 {
     print "<li>$birds</li>";
 }
 print "</ul>";
 
 print "<h1>Finch Now Added to List</h1>";
 $birdArray[] = 'Finch';
 
 print "<ul>";
 FOREACH($birdArray as $birds)
 {
     print "<li>$birds</li>";
 }
 print "</ul>";
 
 ?>
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Dante2
1

Have a look at the docs page for all array functions. I'm sure you can think of one you can use (Perhaps even more).

Votes + Comments
Gave a good answer while allowing the student to figure it out or come to their own conclusions.
1

Thanks man! Found just what I was looking for.

Array_unshift()

did the trick.

Votes + Comments
Good job.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.