November 16, 2011 Composition of an array

Arrays in PHP made easy

Arrays in PHP made easy

It has been mentioned in previous posts, all about scalar variables. These are the ‘normal’ variables we are able to use in a PHP program. An array is somewhat similar to a variable, in that it takes a value, but not just one value, it can take many values. You can say that this type of variable can store a ‘set’ of variables. Let’s say we have a variable named product, and in this variable, you would like to store the product name, product price, and product description. This variable should look something like this:

Composition of an array
Composition of an array

Types of array

Arrays in PHP are a bit different from other programming languages. There are 2 types of arrays that we will see here in PHP. These are numerically indexed arrays, and associative arrays. The indexed arrays, are very common in most programming languages. The indices start at zero by default. This value can be altered if needed.

Initializing Indexed Arrays

To define an array in the simplest way is as follows:

The above example has created an array with the name of products. This array is currently empty, so how do we do about, to give this some data? There are a few ways to achieve this goal. Let’s see how this can be accomplished:

Another way to assign values to specific index, can be done as follows:

If we were to get the values from this same indexed array, then we would need to use the variable with their respective index. For example, We have created this variable, and we know that those index are 0, 1, and 2. The correspond to Name, Price, and Description. To get the values, we would do as follow:

Using Loops

Since our previous example of array variable is an indexed type variable, then we can go ahead and use a for loop to access the values within this array.

You can also use a foreach loop to access the values of an array. This foreach loop was created essentially to facilitate the way we get array values.

Both loops above print out the same results.

Associative Arrays

Let’s take a look at how the associative arrays are in PHP. This is one of my favorite features within PHP. Let’s create the same array variable that we created above, but this time with key indices. These key indices help you identify each value that is within the array.

This previous array is exactly the same as our previous example, except that, this time, if I wanted to display the name of the product, then instead of using a number index, I would just access it with its key.

In this case, since the array is not a numbered indexed array, it is not possible to utilize a simple for loop. We need to use the foreach loop, so that all elements can be accessed.

In the above example, we have that the process will output the name of the key equal to the value of that key.

each() and reset() function

We can also achieve the same results using the while loop, but we need to use the PHP function each(). This function, when called, returns the current element, and making the next element, the current one.

Note that, when using the each() function, the array will keep track of what the current element is. If you need to process this array after using each(), you would need to reset it to make the current element to the first element. This is accomplished by using the reset() function. Using this function will allow you to go back to the array, and loop through it once again.

There are much more functions that can be used for arrays. For my next post, I will include a few more of those, so that way you can get the hang of PHP arrays. Also, I will include multidemensional array, and how to access them.

Until next time!

One thought on “Arrays in PHP made easy

  1. Thanks for the post. After reading I went to read more about each(). The php manual had some great posts. As a beginner the hardest thing to do is choose which function to use. I use three criteria when I find more than one way to do something.
    1. Speed. Which function is faster. In your examples the foreach loop is much faster than the each.

    2. Code readability. When I am reading code I find it easier to understand a foreach loop.

    3. Debugging. Foreach loops break up functions onto multiple lines making it easier to debug problems.

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.