Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. Arrays > Traversing Arrays

Traversing Arrays

Before we get into the listing of the best array functions, we need to look at ways to easily walk through, or traverse, an array. If you read Chapter 2, you might remember that we skipped over a flow control structure and put it on the shelf until this chapter. That flow control structure is the foreach construct, and it has great value and use in the context of arrays. It will allow you to step through each element of a supplied array and implement almost any code on the key/value pairs. In addition, this construct will place the key and value of each iteration into their own variables. Using our array sample from before, let’s go through each key/value pair and echo each one out onto the screen. We can do that with this code:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint