Multidimensional Arrays In Php

An array is a special variable that we use lớn store or hold more than one value in a single variable without having lớn create more variables lớn store those values.

To create an array in PHP, we use the array function array( ).

Bạn đang xem: Multidimensional Arrays In Php

By default, an array of any variable starts with the 0 index. So whenever you want to điện thoại tư vấn the first value of an array you start with 0 then the next is 1...and so on.

There are different types of arrays in PHP. They are:

Numeric/Indexed ArraysAssociative ArraysMultidimensional Arrays

Let"s look at how each one works in more detail.

What are Numerical or Indexed Arrays?

A numerical array is a type of array which can store strings, numbers, & objects. Here"s an example of a numeric array:

From the code above I have a variable of $cars which stores an array of 5 elements. The var_dump($cars) từ khoá above will show us the total number of elements we have in the array, the index number of each array, and also the length of each element in the array.

You can also chose khổng lồ use the echo( ) keyword, but in my case I prefer lớn use var_dump( ) because it gives a more detailed explanation of the results we get.


You can also choose khổng lồ display only one element/item of an array in the web browser by doing this:

The code above follows the same pattern as our definition of an array, which states that it counts from zero. We want khổng lồ display the element with the index of 4. Counting from 0 lớn 4, we can see that 88 falls under index 4, indicating that 88 is the number we"re seeking and that will be displayed khổng lồ the browser.


What are Associative Arrays?

An associative array is a type of array where the key has its own value. In an associative array, we make use of key và value.

Keys are descriptive captions of the array element used lớn access the value of the array. And value is the value assigned to the array element.

Xem thêm: Bí Quyết Học Tiếng Anh Của Khánh Vy

There are situations where you shouldn"t use the numeric/indexed array, such as:

When you want khổng lồ store the age of different students along with their names.When you want to lớn record the salaries of your employees.When you want khổng lồ store the score of a student in different subjects

and so on.

Suppose we want to assign ages to a group of high school students with their names.

We can use the Associative array method to get it done. For example:

17,"Stalenski" => 18,"Lydia" => 16,"Allision" => 17,);echo $student_age <"Scott_Mcall">; //this code will display the age of Scot_Mcall as 17echo $student_age <"Stalenski">; //this code will display the age of stalenski as 18echo $student_age <"Lydia">; //this code will display the age of Lydia as 16echo $student_age <"Allision">; //this code will display the age of Allision as 17?>The code above is an example of an associative array. The keys of the array are scott_Mcall, Stalenski, Lydia, Allision, & we used them to lớn assign the age to each student. The values of the array are 17, 18, 16, and 17.

What are Multidimensional Arrays?

You can think of a multidimensional array as an array of arrays. This means that every element in the array holds a sub-array within it. In general, multidimensional arrays allow you khổng lồ store multiple arrays in a single variable.

Suppose we want to store the Names, Registration Numbers, and Emails of some of the staff working in a particular company. We can use multidimensional arrays to lớn archive this.

For example:

"Derek Emmanuel","Reg_No" => "FE/30304","Email" => "derekemmanuel">,<"Name" => "Rubecca Michealson","Reg_No" => "FE/20003","Email" => "">,<"Name" => "Frank Castle","Reg_No" => "FE/10002","Email" => "fcastle86">>;echo $Staffs <2> <"Email">; // This displays the email of the last staff which is fcastle86
gmail.comecho $staffs <0> <"Name">; //This displays the Name of the staff in the first array (index 0) which is Derek Emmanuel // you can access the information of any staff you wish to lớn by using echo $(variable name) <"array element key">.?>Remember, an array starts counting from index 0. The code above is an example of a multidimensional array because it contains more than one array (an array of arrays) with one single variable of $staff.

The echo $staff <2> <‘Email’> displays the e-mail of the staff that falls into the index of 2. In our case it will display fcastle86

If I want khổng lồ access the email of the staff in the first array, we"ll bởi vì the following:

echo $staff <0> <"Email">;

Using the method above, you can access & display any information in the array from the code above.


At this point you should be able to use the three different types of arrays when working on a PHP project.