Updated on 03 Nov 2019 | 4 Min Read
By
 

PHP (Hypertext Preprocessor) is a powerful tool or framework used for making dynamic and interactive web pages. It works on client-server architecture in which servers accept request and reply response in HTML format. Most of the developers use PHP framework for- code security; creation of dynamic web pages; database connectivity, etc. To Crete web pages most of the developers prefer to use PHP because it runs on different platforms (Windows, Linux, Unix, etc.). It is compatible with almost all servers that are used today namely IIS, Apache and various others. In PHP, functions are the most widely used concepts which make the framework highly usable and interactive. In general, functions are the group of statements or programs that collaboratively execute as a single unit to serve a specific functionality. In this article, various PHP array functions are discussed which will help you to crack PHP interviews. The PHP framework is comprised of various array functions that allow the developer to interact and manipulate the arrays in various ways.

PHP Array Functions

In general, PHP array functions are the number of miscellaneous lines/statements that can be integrated to execute as a single unit. Given below is the syntax used for defining a function

function name function(parameters) {
   Statements;
}

The PHP array functions with examples that allow you to interact with and manipulate arrays in various ways. Arrays are most probably used for managing, storing and operating the variables. Given below is the list of array functions in PHP.

1. is_array ($arr)

In order to check whether the provided information and data are in the form of an array or not, the array function namely is_array() is used.

Syntax-
$lamborghinis = array ("Urus", "Huracan", "Aventador");
echo is_array($lamborghinis) ? 'Array' : 'not an Array';
$mycar = "Urus";
echo is_array($mycar) ? 'Array' : 'not an Array';

Output: not an array

2. sizeof($arr)

In order to access the size of the array sizeof() is used. In addition to this in order to evaluate the number of variables or data stored in the array this function is employed. This functionality of this function resembles with count () method. Thus in order to traverse the array for counting the stored values sizeof() function is most preferably used by the developers.
Syntax-
$lamborghinis = array ("Urus", "Huracan", "Aventador");
echo "Size of the array is: ". sizeof($lamborghinis);

Output: Size of the array is: 3

3. In_array ($var, $arr)

In order to check whether a specific value is present in the array or not the in_array($var, $arr). For example, if there is a list of cars and we want to check a specific array from the list of arrays then we use in_array () function.
Syntax-
$lamborghinis = array ("Best", "Interview", "Questions");
$concept = "Questions";
echo in_array($concept, $lamborghinis) ? 'Yes' : 'Not'

Output: Yes

4. print_r($arr)

This array function is used to print the value of array in a well-alienated manner. This function is used to print the array values in the most descriptive way. the complete representation of array is printed using print() function.

$lamborghinis = array ("Urus", "Huracan", "Aventador");
print_r($lamborghinis);

Output:
Array (
[0] => "Urus"
[1] => "Huracan"
[2] => "Aventador"
)

Related Articles: PHP Strings

5. array_merge ($arr1, $arr2)

In order to integrate or combine the two different arrays into a single array this array function is used. By using array_merge ($arr, $arr) function you can combine different indexed arrays into single array.

Syntax-
$hatchbacks = array(
"Suzuki" => "Baleno",
"Skoda" => "Fabia",
"Hyundai" => "i20",
"Tata" => "Tigor"
);
$friends = array("Vinod", "Javed", "Navjot", "Samuel");
$merged = array_merge($hatchbacks, $friends);
print_r($merged);

Output-
Array (
[Suzuki] => Baleno
[Skoda] => Fabia
[Hyundai] => i20
[Tata] => Tigor
[0] => Vinod
[1] => Javed
[2] => Navjot
[3] => Samuel
)

6. array_values ($arr)

In case of array, the variables are stored in key-value pairs. If the users want all the values from the array without keys then array_values () fnction is used. This array function used to excat the va;ue from the array with the keys. Given below is the syntax of the array.

Syntax-
$hatchbacks = array(
"Suzuki" => "Baleno",
"Skoda" => "Fabia",
"Hyundai" => "i20",
"Tata" => "Tigor"
);
$friends = array("Vinod", "Javed", "Navjot", "Samuel");
$merged = array_merge($hatchbacks, $friends);
$merged = array_values($merged);
print_r($merged);

Output-
Array (
[0] => Baleno
[1] => Fabia
[2] => i20
[3] => Tigor
[4] => Vinod
[5] => Javed
[6] => Navjot
[7] => Samuel
)

7. array_keys ($arr)

As discussed above the function array_values ($arr) is used to access the value from value-key paired. Similarly, array_keys ($arr) function is used to access keys from the value key pairs without accessing the values.

Syntax-
$keys = array_values($merged);
print_r($keys);

Output-
Array (
[0] => Suzuki
[1] => Skoda
[2] => Hyundai
[3] => Tata
[4] => 0
[5] => 1
[6] => 2
[7] => 3
)

8. array_pop ($arr)

This array function is preliminarily used remove the last element from the list of array structure. $lamborghinis = array("Urus", "Huracan", "Aventador");
array_pop($lamborghinis);
print_r($lamborghinis);

Output-
Array (
[0] => Urus
[1] => Huracan
)

9. array_push ($arr, $va1)

This array function is preliminarily used add the last element to the list of array structures. Sometime, it might be possible that user want to add the elements in the array. Therefore, in order to add elements or to elongate the list of elements this function is employed.

Syntax-
$lamborghinis = array ("Urus", "Huracan", "Aventador");
array_push($lamborghinis, "Estoque");
print_r($lamborghinis);

Output-
Array (
[0] => Urus
[1] => Huracan
[2] => Aventador
[3] => Estoque
)

10. sort ($arr)

In order to sort the array in ascending order in alphabetical order sort () array function is used. This function comes into the role when users want to assign ID numbers to each element which are present in the array structure.
$lamborghinis = array("Urus", "Huracan", "Aventador", "Estoque");
sort($lamborghinis);
print_r($lamborghinis);

Output-
Array (
[0] => Aventador
[1] => Estoque
[2] => Huracan
[3] => Urus
)

Conclusion

In this article, we have collected and selected the best PHP interview questions which will help to crack the interviews. Go through all the basic concepts of arrays of PHP you will increase your chances of success in the interview. You will be clarifying the concept of array and its function in PHP as each type of array is justified by providing examples cases.