PHP Basics Part 4-Array Guide

Sharing is Caring

In this chapter, we are going to go through PHP arrays, for loop, while loop and other related aspects of PHP. We have already covered some ground including php basics, variables and functions.

This is time to take it the slightly next level. if you have gone through the first three chapters of this PHP tutorial, then you must be aware of how variables & functions, if not then you can refer to respective chapters;

PHP Variables

PHP Functions

Now let begin learning php array.

PHP Arrays

The reason I have asked you to read above chapter is that if you learn them you will be able to understand PHP array in a much better way.

PHP arrays are special type of php variable which can store multiple values to it.

Under conventional variable, if you have a list 1000 items, you will have to create variable for all of them. Imagine if you have 1000 variable, it will make your code big and difficult to manage.

PHP array help in simplifying the above task by storing all those 1000 items in a single variable which is called PHP array.

You can not only store multiple values in a single array, but also call or refer to individual value within that array.

There are many php array functions which further add value to it. Some of the php array functions are;

array()–Creates an array

array_change_key_case()–Changes all keys in an array to lowercase or uppercase

array_chunk()–Splits an array into chunks of arrays

array_column()–Returns the values from a single column in the input array

array_combine()–Creates an array by using the elements from one “keys” array and one “values” array

array_count_values()–Counts all the values of an array

array_diff()–Compare arrays, and returns the differences (compare values only)

array_diff_assoc()–Compare arrays, and returns the differences (compare keys and values)

array_diff_key()               –Compare arrays, and returns the differences (compare keys only)

array_diff_uassoc()–Compare arrays, and returns the differences (compare keys and values, using a user-defined key comparison function)

array_diff_ukey()–Compare arrays, and returns the differences (compare keys only, using a user-defined key comparison function)

array_fill()–Fills an array with values

array_fill_keys()               –Fills an array with values, specifying keys

array_filter()–Filters the values of an array using a callback function

array_flip()–Flips/Exchanges all keys with their associated values in an array

array_intersect()–Compare arrays, and returns the matches (compare values only)

array_intersect_assoc()                –Compare arrays and returns the matches (compare keys and values)

array_intersect_key()–Compare arrays, and returns the matches (compare keys only)

array_intersect_uassoc()–Compare arrays, and returns the matches (compare keys and values, using a user-defined key comparison function)

How To Create PHP Arrays

As mentioned earlier that PHP arrays are special variable, so if you know how to create php variable, you can easily create php arrays.

If you look at the above example, then you will know $firstarray is a conventional php variable, its just that in case of a array, after = sign we have to add keyword ‘array’ with parenthesis in which we include all the list.

When we use array, we can call the array itself or the individual component in it by using adding the index number in the bracket like this;

echo “$firstarray[0]”;

You need to remember the first item in a array doesn’t start with the serial number 1, it always start with 0. That how serial numbers are assigned to the items in a array. So we have to call the item5 in the above mentioned array we have to use 6 and it will shown as;

echo “$firstarray[6]”;

Array are vital for any website or php based application. If you are creating a website then you will have many items and array make it easy to manage.

If you want to change the design then simply make changes in your css targeting that one array and you will end up changing the design of every item within it whereas if you create variable for each of them separately then you will have to change each one of them separately which will not only take lot of time but also increases the probably of errors.

In order to work with arrays, you need to understand few additional concepts other than function and variable. They are loops. We will talk about them later.

We first need to go through the types of arrays.

Types of PHP Arrays

  1. Index array
  2. Associated array
  3. Multi-dimensional array

Now let’s go through them;

Indexed Array

Indexed arrays are simple arrays which we have discussed above where you store multiple items in array and each value is assigned a serial number. This serial number is called index in case of indexed arrays.

There are two ways of assigning index value to the items within arrays, one is automatic where php assign a value to the items itself starting from 0 and second method, where coder manually assign a serial number to it, but again starting from 0.

We can use similar examples as above to understand indexed array



Associated Array

An associated array is slightly more advance and useful than indexed array. You may encounter situations where you have to store multiple values paired values in an array.

For example;

Roll number and name of the students

Employee number and name of the employee

Brand name and items name

In such situations, indexed array won’t be sufficient because you cannot store paired values in it.

Associative array helps in doing exactly that. Associative array use named keys assigned to values using a => sign. So say you want to store a employee name with its employee number, you will put it like;


Now let’s look at a example of associative array to understand how it look and function;


Multi-Dimensional Array

Indexed arrays are used to store single values, associative arrays are used to store paired values but what if you want to store something bigger? You have to use multi-dimensional arrays to do that. Multi-dimensional arrays use multiple arrays within an array.


Multi-Dimensional arrays are extremely useful in storing complicated and bigger data & variables.

PHP Array Conclusion

This is the introduction of PHP arrays. In the next few chapters we will further go deep down in arrays. But in order to get better grip on these basic concepts, it is better to start from the scratch.

You will have to use ‘for each loop’ to loop through the keys and values within an array.

We will cover loops in the next chapter and as we move ahead, we will use already covered concepts more & more so that everything fall in place to complete the php learning.

Sharing is Caring
About akhilendra

Hi, I’m Akhilendra and I write about Business Analysis, Data Science, IT & Web. Join me on Twitter, Facebook & Google+


  1. Thanks for this simple and great article from a newbie point of view.

  2. Thank you Akhilendra , for explaining this very useful and important part of PHP, this will be very helpful to the beginners , the way you have explained the functions and arrays is really good that anyone can understand and apply those…I would like to read more of your posts, i will increase my knowledge /

  3. This information was more valuable and authentic than others.I would like to visit your fresh posts to collect latest information. Please have a generous look on my website to see,watch and share funny content.thank you.

Speak Your Mind