# PHP Arrays

An **array** stores multiple values in one single variable. It is one of the most versatile and commonly used data structures in PHP.

If a variable is a single box, an array is a filing cabinet.

---

## 1. Indexed Arrays
Arrays with a numeric index. The index automatically starts at 0.

```php
$cars = ["Volvo", "BMW", "Toyota"];
echo "I like " . $cars[0] . ".";
```

## 2. Associative Arrays
Arrays with named keys that you assign to them. This is similar to a Dictionary or Hash Map in other languages.

```php
$ages = ["Peter" => 35, "Ben" => 37, "Joe" => 43];
echo "Peter is " . $ages['Peter'] . " years old.";
```

## 3. Multidimensional Arrays
Arrays containing one or more arrays.

```php
$cars = [
  ["Volvo", 22, 18],
  ["BMW", 15, 13],
  ["Saab", 5, 2]
];
echo $cars[0][0] . ": In stock: " . $cars[0][1];
```

## 4. Common Functions
*   `count($arr)`: Get the number of elements.
*   `array_push($arr, $val)`: Add element to end.
*   `array_pop($arr)`: Remove element from end.
*   `foreach($arr as $item)`: Loop through the array.

[[programming/php/php]]