Skip to Content
Learning PHP
book

Learning PHP

by David Sklar
April 2016
Beginner content levelBeginner
550 pages
9h 40m
English
O'Reilly Media, Inc.
Content preview from Learning PHP

Chapter 4. Groups of Data: Working with Arrays

Arrays are collections of related values, such as the data submitted from a form, the names of students in a class, or the populations of a list of cities. In Chapter 2, you learned that a variable is a named container that holds a value. An array is a container that holds multiple values.

This chapter shows you how to work with arrays. The next section, “Array Basics”, goes over fundamentals such as how to create arrays and manipulate their elements. Frequently, you’ll want to do something with each element in an array, such as print it or inspect it for certain conditions. “Looping Through Arrays” explains how to do these things with the foreach() and for() constructs. “Modifying Arrays” introduces the implode() and explode() functions, which turn arrays into strings and strings into arrays. Another kind of array modification is sorting, which is discussed in “Sorting Arrays”. Finally, “Using Multidimensional Arrays” explores arrays that contain other arrays.

Working with arrays is a common PHP programming task. Chapter 7 shows you how to process form data, which the PHP engine automatically puts into an array for you. When you retrieve information from a database as described in Chapter 8, that data is often packaged into an array. Being comfortable with arrays makes it easy for you to manipulate these kinds of data.

Array Basics

An array is made up of elements. Each element has a key and a value. For example, an array holding ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning PHP 5

Learning PHP 5

David Sklar
Learning PHP 7

Learning PHP 7

Antonio L Zapata (GBP)
Advanced PHP Programming

Advanced PHP Programming

George Schlossnagle

Publisher Resources

ISBN: 9781491933565Errata PageSupplemental Content