Using PHP's array() Function to Create Indexed and Associative Arrays
This article explains PHP's built‑in array() function, demonstrating how to create both indexed and associative arrays with clear examples, code snippets, and important usage notes for developers. It also covers mixed and nested arrays, parameter rules, and common pitfalls to help you write robust PHP code.
Function Overview
The array() function is a built‑in PHP function used to create arrays. It accepts any number of arguments and can produce either an indexed array or an associative array based on the supplied values.
Creating Indexed Arrays
Indexed arrays are the default and most common array type in PHP. To create one, simply pass the values to array():
$names = array("John", "Jane", "David", "Alice");In this example, $names is an indexed array containing four elements. Accessing elements uses zero‑based indexes, e.g., $names[0] yields "John" and $names[1] yields "Jane".
Creating Associative Arrays
Associative arrays use custom keys that map to values. Keys and values are separated by the => operator and passed to array():
$person = array("name" => "John", "age" => 25, "gender" => "male");The resulting $person array contains three key‑value pairs. Values can be accessed via their keys, such as $person["name"] returning "John" and $person["age"] returning 25.
Important Considerations
Parameter count is unlimited; array() can receive any number of arguments separated by commas.
Parameter types are flexible: strings, integers, floats, or any variable type can be used.
Indexed array keys start at 0 and increment automatically; associative array keys can be strings or integers.
If both string and numeric keys are supplied, PHP converts numeric keys to integers automatically.
Example Code
Below are several practical examples illustrating 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 both 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 in arrays
echo $fruits[1]; // outputs: Banana
echo $person["name"]; // outputs: JohnPHP Learning Recommendations
For further study, consider the following resources:
Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial
Vue3+TP6+API Social E‑commerce System Development Course
Swoole From Beginner to Expert Course
Workerman+TP6 Real‑time Chat System (Limited Time Offer)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
