Using PHP's array() Function to Create Indexed and Associative Arrays

This article explains PHP's built‑in array() function, demonstrates how to create both indexed and associative arrays with clear code examples, outlines important usage notes, and provides additional sample snippets to help developers master array creation in backend development.

php Courses
php Courses
php Courses
Using PHP's array() Function to Create Indexed and Associative Arrays

In PHP, the array() function is a widely used built‑in function for creating arrays; it accepts any number of arguments and can generate either indexed or associative arrays.

Function Overview

The array() function can build an indexed array (numeric keys starting at 0) or an associative array (custom string or integer keys) depending on how the arguments are supplied.

Creating Indexed Arrays

To create an indexed array, simply pass the values to array(). Example:

$names = array("John", "Jane", "David", "Alice");

The resulting array $names contains four elements, accessible as $names[0] ("John"), $names[1] ("Jane"), and so on.

Creating Associative Arrays

Associative arrays use custom keys paired with values, separated by the => operator. Example:

$person = array("name" => "John", "age" => 25, "gender" => "male");

The array $person holds three key‑value pairs; values can be accessed via $person["name"] ("John") and $person["age"] (25).

Important Considerations

Number of parameters is unlimited; you can pass as many values as needed.

Parameter types can be strings, integers, floats, or any variable type.

Indexed array keys start at 0 and increment; associative array keys can be strings or integers.

If both string and numeric keys are mixed, PHP converts numeric keys to integers automatically.

Sample Code

Additional examples illustrate creating empty arrays, mixed arrays, and nested arrays, as well as accessing elements:

// 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 (indexed + associative)
$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: John

Conclusion

The array() function is essential for PHP developers; whether building simple indexed lists or complex associative structures, mastering its usage greatly simplifies data handling in backend applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ArrayTutorial
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.