PHP has another type of arrays which we call as Associative Arrays. An Associate Array is object-indexed collection of objects.

Its similar to previous definition but it does not say that it is ordered anymore and instead of being integer-indexed , it is object-indexed i.e it is going to be indexed by a label of some sort.

Associative Arrays in PHP

Now instead of being ordered, they are labeled. Instead of saying our phone bill is in Pocket 1 and Electricity bill in Pocket 2, we are going to say find the pocket labeled as Phone Bill.

We call the label as Key and the content of the pocket as value and together they are called as Key-Value Pair.

Associative Arrays in PHP
Basic Array Associative Array
When order is important to us, we are going to use Basic Arrays.

 

When order is not important to us or when having a reference label is important, then we are going to use Associative Arrays.

 

Fg: If we have data of 100 customers, we are going to store them in an array as the order of the customer matters.

 

Fg: To store the information about each customer, we are going to use Associative Array.

 

Program 20

Now, assignment works in similar way as we done it in regular arrays.

Program 21

Now numbers don’t work anymore in Associative Arrays. So it will give us an error as it does not have any index as 0.

Any object does not have to be a string. It can also be a number. So in the following example both of the arrays are similar.

Program 22

 

So it is clear from the above that both the arrays are very similar to each other. In second array, we can change the index from ‘2’ to ‘20’. It’s perfectly alright.

Arrays plays a very important role in PHP, so it is necessary that you clearly understands it.

Array Functions

In this topic, we are going to cover Array Functions. There are lot of different array functions which we can use in a lot of content. So we are going to study a few of them, so that we can understand how to use them.

So let us start by taking a look at some examples.

Program 23

 

Now let’s try Sort And Reverse Sort

Program 24

Next we are going to study how we can convert an array into a string. To do that we are going to study Implode Function

Program 25

The first argument “*” is the separator we are going to use to separate strings. The opposite of Implode function is the Explode Function.

Program 26

Next, we are going to study to check whether a particular thing is present in an array or not.

Program 27

So we are going to check whether 15 or 19 is present in an array or not. It is going to give us the result either in True or False.

Now I already told that there are lot of different functions in an array. So you can visit php.net website to study them all.

http://php.net/manual/en/ref.array.php