![]() |
Hall of Famer's PHP Tutorials: Chapter 6 - PHP Arrays
Chapter VI - PHP Arrays In Chapter 5, you learned the basics of PHP functions. Now we are ready to move on to the study of some widely used PHP data types/structures. These aint as powerful as Java's, but will come in handy. I am sure you are quite familiar with playing with scalar variables like numbers and strings, so now it is about time to move on to vector variables. This chapter is about PHP arrays. Arrays are very useful data type in any programming languages, they are a collection of elements with each element having its own identity. In PHP, array can behave both like a traditional array in Java/C#, and like a hashmap, which makes it more powerful than typical arrays from another programming language. The tutorial will teach you how to create and manipulate arrays, and then introduce a few useful built-in PHP array functions. If you are not familiar with how to use functions, it may be a good time to go back to chapter 5 and read the function basics section. 1. Creating a numeric Array: To create a numeric array, you typically use the array keyword with elements enclosed in parenthesis. Below is a standard way to declare arrays: PHP Code:
Since PHP 5.4, there is a shortcut syntax to define PHP arrays in similar way as javascript arrays. The syntax is simple, you enclose your array elements in square bracket []. You do, however, need to make sure that you are running at PHP 5.4 or above: PHP Code:
2. Creating an associative array: An associative array, abbreviated as assoc, is what normally appear in most general purpose programming languages as hash map. The difference between an associative array and a numeric array is that it uses string as index instead of numbers. The string is given a unique name called 'key', which also serves as identifier for a specific array element. The key-value pair are easy to use in many cases. To declare an associative array, use the symbol => to map a key to a value: PHP Code:
3. Manipulating Array indexes and keys: So far we have been able to create numeric and associative arrays, but they aint quite useful yet. We will need a way to reference to the array's element in a given index for numeric array, or key-value pair for associative array. To accomplish this, we simply use the square bracket to enclose the index number of key string. It is very easy to accomplish, the below example prints out the value for both $array and $assoc: PHP Code:
Adding or overwriting elements/key-value pairs is also a straightforward task. After an array is created, you may just write the variable array followed by square bracket to create/edit array elements/key-value pairs. The below syntax illustrates how this can be done: PHP Code:
4. 'foreach' Loop on PHP Arrays: Back in chapter 4 iterations/loops, I briefly introduced three basic loop structures that you can easily play with. There is another one that I chose not to bring up, since it involves arrays. It is perfect time to get into this 'foreach' loop structure since we are in the world of arrays now. Since an array is a collection of elements, we will need to a way to iterate through its elements by following any possible order. We can use a while or for loop here, but we will need to keep track of the index or the current key, sometimes its easy to get lost. For associative arrays, it is an almost impossible task. With foreach loop, the work is greatly simplified as it is specifically designed for vector data type operations. To create a foreach loop, the syntax is foreach($array as $element) for numeric arrays, and foreach($array as $key => $value) for associative arrays. The below code demonstrates how it actually works: PHP Code:
5. Some useful built-in Array functions: The syntax of creating/manipulating arrays aint quite complex, but it is a different story if you need more powerful tools to play with arrays. There are a few commonly used Array functions that come bundled with PHP language itself, I will introduce a few of them to you here. First of all, you can use the count() function to get the size of the array. It is the simplest function for array manipulation but it also is the most commonly used. PHP Code:
PHP Code:
Third, you can manipulate arrays by pushing a bunch of elements into array, merging two arrays together, or shifting the first element out from the array: PHP Code:
PHP Code:
6. Multi-dimensional Arrays: Another advanced feature about PHP arrays is that you can have an array whose elements are arrays, and thus making it a multi-dimensional array. The simplest example is a user collection array in which you have the first dimension of a collection being the username, and the second dimension being the user's detailed information such as ID, email and age. Below is an example of such multi-dimensional array: PHP Code:
This concludes the introduction to PHP arrays, hopefully you find it useful. As you can see from the examples provided above, PHP arrays are nothing complicated and should be easily understood if you've come this far. Array is one of the vector data types in PHP, the other is object which will be brought up for intermediate-skilled programmers tutorials that I will write after finishing up the entire beginners guides. The next chapter will discuss PHP strings, which should be straightforward too. Until then, have fun playing with the script and see how much you can do about it with the current level of knowledge you've learned from my tutorials. |
All times are GMT -5. The time now is 04:01 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.