Arrays in PHP
An array is a variable that can hold multiple values.
1. Indexed Arrays
Values stored with numeric keys.
Or using array():
2. Associative Arrays
Values stored with custom keys.
3. Multidimensional Arrays
Arrays within arrays.
4. Useful Array Functions
| Function | Description |
|---|---|
count() |
Get the number of elements |
array_push() |
Add item(s) to end of array |
array_pop() |
Remove last element |
array_keys() |
Get all keys |
in_array() |
Check if a value exists |
sort() |
Sort array in ascending order |
asort() |
Sort associative array by value |
Functions in PHP
Functions help you reuse blocks of code.
1. Defining a Function
2. Functions with Parameters
3. Functions with Return Values
4. Passing Arrays to Functions
5. Default Parameters
Summary
| Feature | Description |
|---|---|
| Indexed array | Uses numeric indexes |
| Assoc. array | Uses named keys |
| Function | Reusable block of code |
| Return | Send a result back from a function |
| Array + Func | Combine both to handle lists flexibly |
