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.
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.
array();
Now we will see why there is a need to use Array.
<?php
$quiz = array("HTML", "PHP", "CSS");
echo $quiz[0] . ", " . $quiz[1] . ", ". $quiz[2];
?>
You think that's why use as [0] for echo result, because the array index starts from 0.
Result of the above code will like as below.
HTML, PHP, CSS
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:
<?php
$array_count = array("I", "Like", "Quizzes");
echo count($array_count );
?>
Result is 3.
Why result is 3, because in the array there are 3 values, 1. I, 2. Like, 3. Quizzes.
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.
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:
$myArray = array("HTML", "CSS", "PHP", "Bootstrap");
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.
// Using array() function
$data = array(
'name' => 'sachin',
'age' => 50,
'website' => 'https://quizlancer.com/'
);
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:
echo $data['name']; // Outputs: sachin
echo $data['age']; // Outputs: 50
echo $data['website']; // Outputs: https://quizlancer.com/
#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.
// Example of two-dimensional array
$myArray = array(
array(a, b, c),
array(d, e, f),
array(g, h, i)
);
// Accessing the elements of the array
echo $myArray[0][0]; // Outputs a
echo $myArray[1][1]; // Outputs e
echo $myArray[2][2]; // Outputs i
![Which of the following functions is used to add an element to the end of an array in PHP?](https://i0.wp.com/quizlancer.com/wp-content/uploads/2024/07/Which-of-the-following-functions-is-used-to-add-an-element-to-the-end-of-an-array-in-PHP.jpg?resize=576%2C1024&ssl=1)
![Which of the following functions is used to add an element to the end of an array in PHP?](https://i0.wp.com/quizlancer.com/wp-content/uploads/2024/07/Which-of-the-following-functions-is-used-to-add-an-element-to-the-end-of-an-array-in-PHP.jpg?resize=576%2C1024&ssl=1)
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:
$myArray = array(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5);
sort($myArray);
print_r($myArray);
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.
Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 3
[4] => 3
[5] => 4
[6] => 5
[7] => 5
[8] => 5
[9] => 6
[10] => 9
)
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.