PHP array() Function
Last Updated :
20 Jun, 2023
Improve
The array() function is an inbuilt function in PHP which is used to create an array. There are three types of array in PHP:
php
php
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, ...) ... )
- val: This parameter is used to hold the value of array.
- key: This parameter is used to hold the key value:
<?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";
}
?>
Output:
Program 2: This example illustrate the Associative array.
DBMS Algorithm C++ JAVA
<?php
// Declare an associative array
$detail = array( "Name"=>"GeeksforGeeks",
"Address"=>"Noida",
"Type"=>"Educational site");
// Display the output
var_dump ($detail);
?>
Output:
Program 3: This example illustrate the Multidimensional array.
array(3) { ["Name"]=> string(13) "GeeksforGeeks" ["Address"]=> string(5) "Noida" ["Type"]=> string(16) "Educational site" }
<?php
// Declare 2D array
$detail = array(array(1, 2, 3, 4),
array(5, 6, 7, 8));
// Display the output
var_dump ($detail);
?>
Output:
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) } }