PHP Arrays
✅ PHP Arrays
An array in PHP is a special variable that can hold multiple values under a single name.
Each value in an array is called an element, and each element has an index (key).
🔹 Types of Arrays in PHP
PHP supports three main types of arrays:
-
Indexed Arrays (numerical index)
-
Associative Arrays (named keys)
-
Multidimensional Arrays (array inside array)
⭐ 1. Indexed Arrays
These arrays use numeric indexes starting from 0.
Creating Indexed Arrays
Method 1:
Method 2:
Accessing Array Elements
Adding Elements
Count Elements
⭐ 2. Associative Arrays
These arrays use named keys instead of numeric indexes.
Creating Associative Arrays
Or
Accessing Values
Adding/Updating Values
⭐ 3. Multidimensional Arrays
An array containing one or more arrays.
Example: 2D Array
Accessing Elements
⭐ 4. Looping Through Arrays
Indexed Array Loop
Associative Array Loop
⭐ 5. Useful Array Functions
array_push() — Add elements
array_pop() — Remove last element
array_shift() — Remove first element
array_unshift() — Add elements at beginning
in_array() — Check if value exists
array_keys() / array_values()
sort() — Sort indexed array (ASC)
asort() — Sort associative array by values
ksort() — Sort associative array by keys
⭐ 6. Associative + Multi-Dimensional Example
🎉 Summary
| Type | Example |
|---|---|
| Indexed Array | $a = ["Apple", "Banana"]; |
| Associative Array | $age["Peter"] = 35; |
| Multidimensional Array | $students[0][1]; |
Arrays are one of the most important parts of PHP programming.
✔ Next topic?
Tell me which topic you want next:
-
PHP Superglobals
-
PHP Form Handling
-
PHP Array Functions (detailed)
-
PHP OOP
-
PHP MySQL
