Arrays

So far we've looked at the basic variables types such as strings and integers, as well as a variety of functions you can use to manipulate these data types. Beyond the basic data types are arrays and objects, which take a little bit more work in order for you to take advantage of them properly. More importantly, arrays take a solid understanding of functions before you try to use them, which is why they are separated here! As mentioned, objects are covered exclusively in their own chapter - this chapter is dedicated to the topic of arrays.

In order to model our surroundings in a programming environment accurately, it is important to recognise that some types of data naturally group together - colours, for example, naturally clump together into one group. Rather than having hundreds of separate variables - one for each colour - the reality is that it makes more sense to have one variable that holds a list, or array of colours.

Topics covered in this chapter are:

  • Reading arrays

  • Manipulating arrays

  • Multidimensional arrays (arrays of arrays)

  • Saving arrays

Chapter contents

  1. 5.1. First steps
  2. 5.2. Associative arrays
  3. 5.3. The two ways of iterating through arrays
  4. 5.4. The array operator
  5. 5.5. Returning arrays from functions
  6. 5.6. Array-specific functions
    1. 5.6.1. Chopping and changing arrays
    2. 5.6.2. Stripping out duplicate values
    3. 5.6.3. Filtering your array through a function
    4. 5.6.4. Converting an array to individual variables
    5. 5.6.5. Checking whether an element exists
    6. 5.6.6. Using an array as a double-ended queue
    7. 5.6.7. Swapping keys and values
    8. 5.6.8. Sorting arrays
    9. 5.6.9. Grabbing keys and values
    10. 5.6.10. Randomising your array
    11. 5.6.11. Creating an array of numbers
  7. 5.7. Multidimensional arrays
  8. 5.8. The array cursor
  9. 5.9. Holes in arrays
  10. 5.10. Arrays in strings
  11. 5.11. Saving arrays
  12. 5.12. Summary
  13. 5.13. Exercises
  14. 5.14. Further reading
  15. 5.15. Next chapter

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: First steps >>

Previous chapter: Next chapter

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.