How to make an array php

Create PHP array w/example: Learn how to create and manipulate an array in PHP, incl. how to access, add, and remove elements.

Creating an Array in PHP

An array is a data structure used in PHP to store multiple values in one single variable. This is particularly useful when you want to save a list of items, such as names or numbers, in a single variable. There are two different ways of creating an array in PHP: using the array() function or using the square brackets notation.

Using the Array Function

The array() function is used to create an array in PHP. The general syntax of the array() function is as follows:

$array_name = array(value1, value2, value3, ...);

Where $array_name is the name of the array, and value1, value2, value3, ... are the values that will be stored in the array. For example, if we want to create an array of names, we can use the following code:

$names = array("John", "Mary", "David", "Peter", "Sarah");

The result of this code is an array with 5 elements, which we can access using their respective index, starting at 0:

echo $names[0]; // Outputs "John"
echo $names[1]; // Outputs "Mary"
echo $names[2]; // Outputs "David"
echo $names[3]; // Outputs "Peter"
echo $names[4]; // Outputs "Sarah"

We can also create an associative array, which is an array in which the keys are not necessarily numerical. For example, we can create an array of people's ages, with their names as the keys, as follows:

$ages = array("John" => 25, "Mary" => 30, "David" => 35, "Peter" => 40, "Sarah" => 45);

Then, we can access the values in the array using the corresponding keys:

echo $ages["John"]; // Outputs 25
echo $ages["Mary"]; // Outputs 30
echo $ages["David"]; // Outputs 35
echo $ages["Peter"]; // Outputs 40
echo $ages["Sarah"]; // Outputs 45

Using the Square Brackets Notation

The square brackets notation is another way of creating an array in PHP. The general syntax of this notation is as follows:

$array_name = [value1, value2, value3, ...];

This notation is equivalent to the array() function, with the same result. For example, we can create the same array of names as we did before, using the square brackets notation:

$names = ["John", "Mary", "David", "Peter", "Sarah"];

The result is the same array, with the same elements and the same indexes, which we can access in the same way as before:

echo $names[0]; // Outputs "John"
echo $names[1]; // Outputs "Mary"
echo $names[2]; // Outputs "David"
echo $names[3]; // Outputs "Peter"
echo $names[4]; // Outputs "Sarah"

We can also use the square brackets notation to create an associative array. For example, we can create the same array of people's ages, with their names as the keys, as follows:

$ages = ["John" => 25, "Mary" => 30, "David" => 35, "Peter" => 40, "Sarah" => 45];

Then, we can access the values in the array using the corresponding keys:

echo $ages["John"]; // Outputs 25
echo $ages["Mary"]; // Outputs 30
echo $ages["David"]; // Outputs 35
echo $ages["Peter"]; // Outputs 40
echo $ages["Sarah"]; // Outputs 45

In conclusion, there are two different ways of creating an array in PHP: using the array() function or using the square brackets notation. Both methods are equivalent, so it is up to you to choose the one that you prefer.

Answers (0)