PHP Tutorial 8: One and Multi-Dimensional Arrays

In previous article we learned about some basics in PHP, this article guides you on how to use arrays in PHP. In Fact, arrays are of important issues in every programming language, In PHP arrays are having more fun than others.

Talking on “arrays in PHP”:

If you remember we discussed about variables in PHP in previous sessions. Variables can hold a value and you can access the value by the variable name. This method is not profitable if you want to save more than one value and need to access all of them. Arrays can help you in such situations. Arrays can hold many related values based on the indexes and you can access theme individually or group-vice.

Arrays actually are variables that can hold many instead of one value.

One-Dimensional Arrays:

For declaring an array, you need to put a $ sign and a name following the sign. After giving a name to array put = sign and right in front of that put the reserved word array with brackets and array members inside the brackets. An array declared in the below example:

As you can see we have declared an array containing three values “Codejow”, “Rojow” and “WebJow”. But how can we access these values? For example, you need to access the second value “Rojow”. For this you need to use indexing function of arrays. Indexes in arrays start from 0, means the first data “Codejow” is having 0 index and it increases one unit by other following data. Our declared array is having 3 indexes as 0,1 and 2. As mentioned in the example, if you want to access the second value “Rojow” then you can use the index 1 to access it.

As used above, you can access a member by giving the index to square brackets [] following the array name. Sometimes you need to take indexing in your hand and you try to index with words not numbers so let’s practice associative arrays.

Associative Arrays:

Associative arrays give us the chance to index the array members by ourselves. Obviously you can use strings instead of integers to index the array members. Concentrate on the example below:

In the example above we have entered some details about user and gave them specific indexes like (Company, Started and Job). Members in this case can be accessed by numbered indexes that starts from 0 but as we have given them custom indexes so we can access them by the custom indexes as well.

Yeah, we could access the year by the custom index “Started”. Be accurate with the syntax in-order to use it.

Alternative Syntax for Associative Arrays:

The code above and the previous example corresponds to the same result but only the method and syntax are different. You must now analyze the above code by yourself.

Multi-Dimensional Arrays:

One of the most powerful tools arrays with multi-dimension. PHP can support multi-dimesional arrays and what it means? Better to continue with example.

Suppose that you’ve collected data related to some users from the bank and want to use it. In this case the best you can do is to put the details of a user in an array and later you can access them easily. Multi-to dimension arrays are as one-dimensional arrays but with more steps.

The syntax is as below:

As given above, this time instead of a single data arrays are given as members of the outer array. The syntax looks complicated but you can learn it by practicing more and more so start writing codes and practice multi-dimensional array.

In codes above we have stored information of two users regarding their Name and Company. How to access the members or users’ information? For accessing the info, you have to use the codes like below:

You only need to use two indexes to access any member. The first index is “user1” and the second is “name” that tells the multi-dimensional array that we are looking for user1 with name in “users” array.

Multi-Dimensional arrays only need to practice to better understand it. If you remember in one-dimensional array section we had a title “Alternative Syntax”, you can try an Alternative syntax for Multi-Dimensional arrays as well.

Stay with codejow for more articles on programming languages.

Leave a Reply