What is an Array in PHP? | 3 Important Types of Array

What is an array in PHP

Array is one of the most important concept in PHP. Everybody must know the concept of array who wants to become a developer. Let’s see what is an array in PHP in detail.

Join to Become PHP Web Developer - Future Scope Career Guidance

WhatsApp Group Join Now
Telegram Group Join Now
Follow On Instagram Follow Us

What is an Array in PHP?

An array is a special variable, which can hold more than one value at a time. This means we can store multiple values in a single variable. Let’s see an example.

If you have a list of items (for example, a list of quiz names), storing subject names (HTML, PHP, CSS) in single variables might look like this. Here variable name is $quiz.

$quiz = “HTML”;
$quiz = “PHP”;
$quiz = “CSS”;

Here we have declared the $quiz variable 3 times for storing subject names. Suppose, we have to store 200 subject names, then we have to declare the $quiz variable 200 times. This is a very bad practice for developers. To resolve this issue one solution is there, and that solution is an Array.

How to Create an Array in PHP

In PHP, the array() function is used to create an array.

Now we will see why there is a need to use Array.

Result of the above code will like as below.

Top 5 PHP String Functions You Should Know as a Developer


How to find the length of an array in PHP? | count length of array in PHP | array count php

We get Lenght of an array by using count() function. The count() function is used to return the length (the number of elements) of an array.

Example:


Interview Questions on what is an array in PHP | Quiz for Fresher

Without Array’s concept or knowledge, nobody can become a perfect developer. Array plays a very important part in development. Several questions are asked during the interview. You will be selected as a developer if you answer all the questions. So I have prepared interview questions or quizzes for you. Solve them as often as possible so your concept about Array is clear.

6

PHP Array

1 / 10

1)

How to get a number of elements in an Array?

2 / 10

2)

How to create an empty array in PHP?

3 / 10

3) Is count() and sizeof() functions are same?

4 / 10

4) Which array function is used for sorting array in descending order?

5 / 10

5) ___ is a collection of variables of the same type that are referenced by a common name.

6 / 10

6) What is the use of array_push () function?

7 / 10

7)

What is an array in PHP?

8 / 10

8) How many types of arrays are available in PHP?

9 / 10

9) Which function returns the length (the number of elements) of an array?

10 / 10

10) Which array sort function is used for sorting arrays in ascending order?

Solve the quiz given below as many times as possible. The questions asked in the quiz are the same as the questions asked in the interview. If you solve maximum quizzes your confidence will increase while giving an interview and your interview will be successful. Definitely, you will get selected as a developer.


Types of Array in PHP

There are 3 types of arrays in PHP.

#1. Indexed arrays

An indexed array is an array that uses numerical keys to store its values. The keys are automatically assigned to the elements of the array in sequential order, starting from 0.

Here’s an example of how to create an indexed array in PHP:

In the above example, $myArray is an indexed array, that contains four elements. The first element has a key of 0 and a value of “HTML”, the second element has a key of 1 and a value of “CSS”, the third element has a key of 2 and a value of “PHP” and the third element has a key of 2 and a value of “Bootstrap”.

#2. Associative arrays

An associative array is a type of array that uses named keys instead of numeric indices. It allows you to store data in key-value pairs, where each key is unique and maps to a specific value.

You can create an associative array in PHP using the array() function.

In the above example, $data is an associative array with three keys: ‘name’, ‘age’, and ‘website’, each mapping to a specific value. To access a value in an associative array, you can use the key as an index, like so:

#3. Multidimensional arrays

A multidimensional array is an array that contains one or more arrays as its elements. These arrays can be of any type, including other multidimensional arrays. Multidimensional arrays are useful for representing complex data structures and for hierarchically organizing data.

Which of the following functions is used to add an element to the end of an array in PHP?
Which of the following functions is used to add an element to the end of an array in PHP?

How to sort an array of elements in ascending order in PHP?

In PHP, you can sort an array’s elements in ascending order using the sort() function. Here’s an example:

In the above example, we have an array of numbers called $myArray. We then use the sort() function to sort the elements in ascending order. The print_r() function is then used to display the sorted array.

The output of the above example is as below.


MCQs on what is an Array in PHP

How do you create an indexed array in PHP?

a) $arr = array(“apple”, “banana”, “cherry”);
b) $arr = “apple”, “banana”, “cherry”;
c) $arr = [“apple” => 1, “banana” => 2, “cherry” => 3];
d) $arr = “apple” => 1, “banana” => 2, “cherry” => 3;

Answer: a) $arr = array(“apple”, “banana”, “cherry”);


Which function is used to count the number of elements in an array?

a) count()
b) sizeof()
c) length()
d) elements()
Answer: a) count()


How do you access the value “banana” from the array $arr = array(“apple”, “banana”, “cherry”);?

a) $arr[1]
b) $arr[0]
c) $arr[2]
d) $arr[“banana”]
Answer: a) $arr[1]


Which function is used to merge two or more arrays?
a) merge()
b) array_merge()
c) combine()
d) array_combine()
Answer: b) array_merge()


Follow us on Social Media

Follow Quiz Lancer Telegram Channel to solve daily interview questions on what is an array in php. We share important interview questions which are asked in the interview. Most of the students get good values from this channel and got selected as a developer.


Read This Also


83 / 100
Scroll to Top