Arrays – Manual – PHP

What is an Array?

An array is a data structure that stores a collection of values. In PHP, arrays are objects, and they can be used to store any type of data, including strings, numbers, objects, and other arrays.

Creating an Array

There are several ways to create an array in PHP. One way is to use the array() function. The array() function takes a list of values as its arguments, and it returns an array object. For example:

Code snippet
$array = array("foo", "bar", "baz");

This creates an array with three elements: “foo”, “bar”, and “baz”.

Another way to create an array is to use the [] syntax. The [] syntax is a shorthand for the array() function. For example:

Code snippet
$array = ["foo", "bar", "baz"];

This is the same as the previous example.

Accessing Array Elements

Once you have created an array, you can access its elements using the square bracket notation. For example, to access the first element of the $array array, you would use the following code:

Code snippet
$first_element = $array[0];

This would assign the value of the first element of the array to the variable $first_element.

You can also use the square bracket notation to assign new values to array elements. For example, to change the value of the first element of the $array array to “hello”, you would use the following code:

Code snippet
$array[0] = "hello";

Adding and Removing Array Elements

You can add new elements to an array using the array() function. For example, to add the element “world” to the end of the $array array, you would use the following code:

Code snippet
$array[] = "world";

You can also remove elements from an array using the unset() function. For example, to remove the first element of the $array array, you would use the following code:

Code snippet
unset($array[0]);

Iterating Over Arrays

You can iterate over an array using a for loop. For example, the following code would print out all of the elements of the $array array:

Code snippet
for ($i = 0; $i < count($array); $i++) {
    echo $array[$i] . "\n";
}

Other Array Functions

PHP has a number of other functions that you can use to work with arrays. Some of these functions include:

  • count() – Returns the number of elements in an array.
  • in_array() – Checks if a value exists in an array.
  • sort() – Sorts the elements of an array.
  • ksort() – Sorts the elements of an array by key.
  • array_merge() – Merges two or more arrays together.
  • array_slice() – Returns a slice of an array.
  • array_map() – Applies a function to each element of an array.
  • array_filter() – Filters an array by a certain criteria.
  • array_reduce() – Reduces an array to a single value.

Conclusion

Arrays are a powerful data structure that can be used to store a collection of values. In PHP, arrays are objects, and they can be used to store any type of data, including strings, numbers, objects, and other arrays. There are a number of functions that you can use to work with arrays in PHP.