Q1 What is an array in PHP?
Ans. Array is a specific type of variable that holds the multiple values. There are mainly 3 types of array in PHP. They are listed below:-
(a) Indexed arrays
(b) Associative arrays
(c) Multidimensional arrays
Q2. What is the indexed array in PHP?
Ans. An indexed array is a array in which all the data elements are stored in the numeric indexes. The index number starts from the zero for the first element in the array.
For example : Below is an index array for the days name. We can create index array in two ways:-
(a) The index can be assigned automatically (index always starts at 0), like this:
<?php $days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thrusday', 'Friday', 'Saturday'); ?>
(b) or the index can be assigned manually:
<?php $days = array(0=>'Sunday', 1=>'Monday', 2=>'Tuesday', 3=>'Wednesday', 4=>'Thrusday', 5=>'Friday', 6=>'Saturday'); ?>
Q3. What is the associative array in PHP?
Ans. The array in which a string is used as a key instead of a number is called an associative array.
For example:- Below is an example of my friends with their name as key & age in years as value:-
<?php $friends = array('Ankit'=>27, 'Varun'=>28, 'Gaurav'=>31); ?>
Q4. What is the multidimensional array in PHP?
Ans. The array containing one or more arrays is called a multidimensional array.
For example:- Below is an example of my friends with their name, age & location:
<?php $friends = array (
array("Ankit",27,'Delhi'),
array("Varun",28,'Goa'),
array("Gaurav",31,'Mumbai')
); ?>
Q5. How to get the total number of elements in an Array?
Ans. To get the total number of elements in the array we can use count() or sizeof() function both will give the same result. For example:-
<?php $friends = array('Ankit','Varun','Gaurav');
echo count($friends); // outputs 3
echo sizeof($friends); // outputs 3 ?>
Q6. What is the difference between array_merge and array_combine?
Ans. The array_combine() is used to create a new array by using the value of one array as keys and using the value of another array as values. For example:-
<?php
$cars = array(1=>"Volvo", 3=>"BMW", 5=>"Toyota");
$friends = array(27=>'Ankit', 28=>'Varun', 31=>'Gaurav');
print_r(array_combine($cars,$friends))
?>
//Output is below
Array ( [Volvo] => Ankit [BMW] => Varun [Toyota] => Gaurav )
The array_merge() merges one or more than one array in a way that the value of one array is appended at the end of the first array. For Example:-
<?php
$cars = array(1=>"Volvo", 3=>"BMW", 5=>"Toyota");
$friends = array(27=>'Ankit', 28=>'Varun', 31=>'Gaurav');
print_r(array_merge($cars,$friends));
?>
//Output is below
Array ( [0] => Volvo [1] => BMW [2] => Toyota [3] => Ankit [4] => Varun [5] => Gaurav )
Q7. How to add a new element in the array?
Ans. To add a new element in the array we can use the array_push() function. It appends the new elements in the array at the end. For example:-
<?php
$friends = array('Ankit','Varun', 'Gaurav');
echo 'Output before add new element in the friends array <br>';
print_r($friends);
array_push($friends,'Ram');
echo '<br><br> Output after add new element in the friends array <br>';
print_r($friends)
?>
The output of the above code is given below:-
Output before add new element in the friends array
Array ( [0] => Ankit [1] => Varun [2] => Gaurav )
Output after add new element in the friends array
Array ( [0] => Ankit [1] => Varun [2] => Gaurav [3] => Ram )
Q8. How to delete an element from an array?
Ans. To delete an element from an array we can use the unset() function. For example:-
<?php
$friends = array('Ankit','Varun','Gaurav');
echo 'Before deleting first element from the $friends array:-<br>';
print_r($friends);
unset($friends[0]);
echo '<br>After deleting first element from the $friends array:-<br>';
print_r($friends);
?>
The output of the above code is given below:-
Before deleting first element from the $friends array:-
Array ( [0] => Ankit [1] => Varun [2] => Gaurav )
After deleting first element from the $friends array:-
Array ( [1] => Varun [2] => Gaurav )
Q9. List the sorting functions available in PHP to sort an array?
Ans. The list of PHP functions for Sorting Arrays is given below:-
(a) sort() : This function is used for sorting arrays in ascending order.
(b) resort() : This function is used for sorting arrays in descending order.
(c) asort() : This function sorts associative arrays in ascending order by array value.
(d) ksort() : This function sorts associative arrays in ascending order by array key.
(e) arsort() : This function sorts associative arrays in descending order by array value.
(f) krsort() : This function sorts associative arrays in descending order by array key.
Q10. How to create an empty array?
Ans. We can create an array in PHP using the array() function or just assigning square brackets i.e [] to a variable. For example, let’s create an empty array of days name:-
<?php $days = array() or $days=[]; ?>