The array() function is an inbuilt function in PHP which is used to create an array. There are three types of array in PHP:
- Indexed array: The array which contains numeric index.
Syntax:array( val1, val2, val3, ... )
- Associative array: The array which contains name as keys.
Syntax:array( key=>val, key=>val, key=>value, ... )
- Multidimensional array: The array which contains one or more arrays.
Syntax:array( array( val11, val12, ...) array( val21, val22, ...) ... )
Parameters: This function accepts atmost two parameters as mentioned above and described below:
- val: This parameter is used to hold the value of array.
- key: This parameter is used to hold the key value:
Return Value: This function returns an array of parameters.
Below programs illustrates the array() function in PHP:
Program 1: This example illustrate the Indexed array.
<?php // Create an array $sub = array("DBMS", "Algorithm", "C++", "JAVA"); // Find length of array $len = count( $sub ); // Loop to print array elements for( $i = 0; $i < $len; $i++) { echo $sub[$i] . "\n"; } ?> |
DBMS Algorithm C++ JAVA
Program 2: This example illustrate the Associative array.
<?php // Declare an associative array $detail = array( "Name"=>"GeeksforGeeks", "Address"=>"Noida", "Type"=>"Educational site"); // Display the output var_dump ($detail); ?> |
array(3) {
["Name"]=>
string(13) "GeeksforGeeks"
["Address"]=>
string(5) "Noida"
["Type"]=>
string(16) "Educational site"
}
Program 3: This example illustrate the Multidimensional array.
<?php // Declare 2D array $detail = array(array(1, 2, 3, 4), array(5, 6, 7, 8)); // Display the output var_dump ($detail); ?> |
array(2) {
[0]=>
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
}
[1]=>
array(4) {
[0]=>
int(5)
[1]=>
int(6)
[2]=>
int(7)
[3]=>
int(8)
}
}
Recommended Posts:
- D3.js interpolate.Array() Function
- How to pass a PHP array to a JavaScript function?
- Difference Between indexOf and findIndex function of array
- How to pass an array as a function parameter in JavaScript ?
- PHP | Program to delete an element from array using unset() function
- How to convert Integer array to String array using JavaScript ?
- How to move an array element from one array position to another in JavaScript?
- Difference between array.size() and array.length in JavaScript
- What is the difference between Array.slice() and Array.splice() in JavaScript ?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- How to convert Object's array to an array using JavaScript ?
- How to get the elements of one array which are not present in another array using JavaScript?
- How to check whether an array is subset of another array using JavaScript ?
- What is the difference between array_merge and array + array in PHP?
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- How to merge the first index of an array with the first index of second array?
- ES6 | Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

