Using PHP array() Function to Create Indexed and Associative Arrays
This article explains the PHP array() function, demonstrates how to create both indexed and associative arrays with clear examples, outlines important usage notes, and provides sample code for various array types to help developers master array creation in PHP.
In PHP, the array() function is a commonly used built‑in function for creating arrays; it can accept any number of arguments and generate either indexed or associative arrays.
Function Overview
The array() function can receive an unlimited number of parameters and, based on the supplied values, create either an indexed array or an associative array.
Creating Indexed Arrays
Indexed arrays are the default and most frequently used array type. To create one, simply pass the values directly to the array() function.
Example:
$names = array("John", "Jane", "David", "Alice");In this example we create an indexed array named $names containing four elements, where $names[0] is "John", $names[1] is "Jane", and so on.
Creating Associative Arrays
Associative arrays use custom keys that map to values. Keys and values are separated by the => operator and passed to array() .
Example:
$person = array("name" => "John", "age" => 25, "gender" => "male");Here $person is an associative array with three key‑value pairs: "name" => "John", "age" => 25, and "gender" => "male". Values can be accessed via $person["name"] (returns "John") and $person["age"] (returns 25).
Important Notes
When using the array() function, keep the following points in mind:
Parameter count is unlimited; you can pass any number of arguments separated by commas.
Parameter types are flexible: strings, integers, floats, or other variables are allowed.
Indexed array keys start at 0 and increment; associative array keys can be strings or integers.
If both string and numeric keys are present, PHP automatically converts numeric keys to integers.
Sample Code
Below are several examples showing how to use array() to create different kinds of arrays:
// Create an empty array
$emptyArray = array();
// Create an indexed array
$fruits = array("Apple", "Banana", "Orange");
// Create an associative array
$person = array("name" => "John", "age" => 25, "gender" => "male");
// Create a mixed array containing indexed and associative elements
$mixedArray = array("Apple", "Banana", "color" => "Red", "size" => "Medium");
// Create a nested array
$nestedArray = array(array("Apple", "Banana"), array("Orange", "Grape"));
// Access elements
echo $fruits[1]; // outputs: Banana
echo $person["name"]; // outputs: JohnSummary
This article introduced the PHP array() function, a fundamental tool for creating both indexed and associative arrays. By following the provided examples and notes, developers can efficiently use array() to build various array structures, greatly simplifying PHP development tasks.
PHP beginner tutorial – learn PHP in one week
Scan the QR code to receive free learning materials
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.