Introduction to Arrays in PHP

In the following article ( introduction to arrays in php ) we will talk about arrays in php, Syntax, Types and how to use them. Before we begin “What exactly is an Array ?”

Array is a collection of values which are stored in a single variable. By now you might have seen that you can store characters and numeric values in a variable. But by the use of array, you can store various types of values in a single variable. Lets see the example below:

Specific values in an array can be retrieved using indexes in array . Indexes are  generally a numerical value, but when using an associative arrays, we uses characters to retrieve values  . Let’s have a look at the second example:

As we have seen in the above example, when we use indexes we start its count from zero. As we have two elements in the array ( 345, “String” ) have the indexes 0 and 1 to access the data .

Associative Arrays in PHP

In general the whole array is associative. In the  example above the 0 index is associated with the first element in the array. By default PHP considers that the index is a number, but we can declare the indexes by our own in Associative Array in PHP . Let us see in the example below.

Arrays with Arrays Within (Nested Arrays)

Nested Arrays are used in a lot of times to mathematical calculations like matrix system . Here is an example:

Similarly you can also do this:

Well this is a simple  tutorial with explanation and how to use an array.

Leave a Reply

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