Arrays in PHP – Different Types of Arrays (Introduction)

Arrays in PHP

In our series of PHP Programs for beginners today we are bringing up a new Article on Arrays in PHP. You will learn about Arrays in detail, also a few examples of arrays and different ways to create arrays. Let’s start with the basics first

What are Arrays in PHP?

For beginners, the best definition for newbies is, Arrays are basically a collection of values. You’re binding up a set of values (values may be of Integer types, String types etc) into a single unit. For example, You want to score marks of 50 students in your class. How to do it? One way is to make 50 variables and store each student’s value in those variables but that’s not very efficient to do. We can perform the same task by using Arrays. We will create an Array which can hold marks of 50 students. Therefore we can store and access these marks more easily.

Types of Arrays in PHP

There are mainly 3 types of Arrays in PHP: –

Numerical Associative (or Simply Indexed Arrays)
(Learn How to Create Arrays in PHP)
Associative Array
Multi-Dimensional Array

In Numerical Array, we access data of an array by a numerical set of values (Those are known as index values of an array, which always starts with Zero). For example, there’s an array named as $student : To access values you can use $student[1] or $student[5] etc.

In the Associative array, we don’t access the content of array by their index value, instead, we use key-value structure. Each value is assigned to a Key to be accessed later on. (Don’t worry, if you don’t get a thing of these. We are going to cover all these in details later on).  For example, to print values of an array we can use ‘foreach’ loop like this:

foreach($student as $k=>$v){
echo “$k => $v <BR>” }

Multi-Dimensional arrays are just a collection of normal 1-Direction arrays. In other words, Multi-Dimensional arrays are used to store other arrays.

So this was quite an overview regarding Arrays in PHP and different types of PHP. Remember, The Multidimensional Array is not a different type of Array – It’s just a collection of normal 1D Arrays. We are going to have a lot of articles on all these array types and of course with coding examples. So Stick with us and you will learn PHP.

Learn: Finding Factorial by Taking Input in PHP


Please enter your comment!
Please enter your name here