PHP array() function


The array() function in PHP is used to create an array. Arrays in PHP are versatile data structures that can hold multiple values under a single variable name. This function allows you to define both indexed and associative arrays.

Syntax:

array(mixed $value1, mixed $value2, ..., mixed $valueN)

Parameters:

  • mixed $value1, $value2, ..., $valueN: A variable number of values that you want to include in the array. You can pass as many values as you like, including other arrays.

Return Value:

  • The function returns an array containing the values passed as arguments.

Example 1: Creating an Indexed Array

An indexed array uses numeric keys to store values. Here's how you can create one:

<?php $fruits = array("Apple", "Banana", "Cherry"); // Output the array print_r($fruits); ?>

Output:

Array ( [0] => Apple [1] => Banana [2] => Cherry )

Example 2: Creating an Associative Array

An associative array uses named keys that you assign to them. This allows you to access values using those keys.

<?php $person = array( "name" => "John", "age" => 30, "city" => "New York" ); // Output the array print_r($person); ?>

Output:

Array ( [name] => John [age] => 30 [city] => New York )

Example 3: Creating a Multi-Dimensional Array

You can also create arrays within arrays, known as multi-dimensional arrays. This is useful for storing more complex data structures.

<?php $students = array( array("name" => "Alice", "age" => 22), array("name" => "Bob", "age" => 24), array("name" => "Charlie", "age" => 23) ); // Output the array print_r($students); ?>

Output:

Array ( [0] => Array ( [name] => Alice [age] => 22 ) [1] => Array ( [name] => Bob [age] => 24 ) [2] => Array ( [name] => Charlie [age] => 23 ) )

Example 4: Using array() to Create an Empty Array

You can also create an empty array and populate it later.

<?php $empty_array = array(); // Create an empty array // Add elements to the array $empty_array[] = "First Item"; $empty_array[] = "Second Item"; // Output the array print_r($empty_array); ?>

Output:

Array ( [0] => First Item [1] => Second Item )

Example 5: Nested Arrays

You can create nested arrays, allowing for even more complex data structures.

<?php $library = array( "fiction" => array("The Great Gatsby", "1984"), "non_fiction" => array("Sapiens", "Educated") ); // Output the array print_r($library); ?>

Output:

Array ( [fiction] => Array ( [0] => The Great Gatsby [1] => 1984 ) [non_fiction] => Array ( [0] => Sapiens [1] => Educated ) )

Summary:

  • The array() function is a fundamental method in PHP for creating arrays, which can be indexed or associative.
  • You can store multiple types of data within arrays, including other arrays, allowing for complex data structures.
  • Arrays are commonly used for storing lists of items, configuration options, and more, making them an essential part of PHP programming.

Note:

As of PHP 5.4, you can also use the short array syntax with square brackets to create arrays, which is often preferred for its simplicity:

$fruits = ["Apple", "Banana", "Cherry"];