Development

PHP Array() Function

In this tutorial, we will explore how we can use the array() method in PHP to create an index array and assign elements to it.
Captain Salem 2 min read
PHP Array() Function
php-array-function

PHP

In PHP, an array is a data structure that can hold an ordered list of values. It is a composite data type that allows you to store multiple values of the same data type under a single variable name.

Under the hood, an array in PHP is an ordered map. A map is an associative data type that stores keys mapped to a given value.

PHP Declare Array Using the Array() Function

The array() function in PHP is a built-in function that allows us to create an array. It can take zero or more comma-separated values as arguments and return an array containing those values.

Function Syntax

The following shows the syntax of the array() method in PHP.

array(mixed ...$values): array

Function Parameters

The function values as the parameter. You can have single values separated by a list or a index => values elements separated by a comma.

It is good to keep in mind that if the index of the elements in the array is omitted, the function will automatically generate the indexes starting at index 0.

If the starting index is provided, the next index generated will be the biggest integer index + 1

Function Return Value

The function will return an array of the provided parameters. The parameters can be given an index using the => operator.

Example 1

The following example shows how to use the array() function in PHP to create a simple array.

$myArray = array('MySQL', 'PostgreSQL', 'SQL Server');

In this example, we are creating an array with three elements of string type.

Example 2

As mentioned, we can also use the array() function to create an associative array where each element is identified by a key. We can pass the key value pairs to the function as shown in the example below:

$databases = array(
    'MySQL' => 3306,
    'PostgreSQL' => 5432,
    'SQL Server' => 1433
);

In this case, we create an associative array with three key-value pairs. The keys are the names of the databases while the values are the corresponding port for each.

Example 3

The following example also demonstrates how to use the array() function to create a 1-index based array.

<?php
$db = array(1 => 'MySQL', 'PostgreSQL', 'SQL Server');
?>

And there you have it, a way to create arrays in PHP using the array() method.

NOTE: The array() function is a language construct used to represent literal arrays, and not a regular function.

Example 4

We can also use the array() method to create a multi-dimensional array in PHP. We can do this by nesting one or more arrays inside another array.

An example of a 2d array using the array function is as shown:

$myArray = array(
    array('apple', 'banana', 'cherry'),
    array('red', 'yellow', 'black'),
    array(1, 2, 3)
);

Conclusion

In this post, we explored how we can create a literal and associative arrays using the array() function in PHP.

We hope you enjoyed this post, leave us a comment down below, share and subscribe.

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.