PHP - Arrays
The array() Function
In PHP, an array is a special variable that can hold more than one value at a time. You can create an array using the array()
function. This function takes a list of values and returns an array containing those values. Here's an example:
$fruits = array("apple", "banana", "cherry");
In this example, we have created an array called $fruits
that contains three elements: "apple", "banana", and "cherry".
Using Square Brackets [ ]
You can also create an array using square brackets []
. This method is often used when you want to initialize an array with specific keys and values. Here's an example:
$students = ["Alice" => 25, "Bob" => 30, "Charlie" => 28];
In this example, we have created an associative array called $students
. Each student's name is a key, and their age is the corresponding value.
Types of Arrays in PHP
There are two main types of arrays in PHP: indexed arrays and associative arrays.
- Indexed Arrays: These are arrays where each element has an index starting from 0. The index is automatically assigned by PHP. Here's an example:
$numbers = [10, 20, 30, 40, 50];
- Associative Arrays: These are arrays where each element has a key associated with it. The key is usually a string, but it can also be an integer. Here's an example:
$ages = ["Alice" => 25, "Bob" => 30, "Charlie" => 28];
Accessing the Array Elements
To access the elements of an array, you can use the array index or the key. Here's how you can do it:
Accessing Indexed Array Elements
echo $numbers[0]; // Output: 10
echo $numbers[2]; // Output: 30
Accessing Associative Array Elements
echo $ages["Alice"]; // Output: 25
echo $ages["Bob"]; // Output: 30
Iterating Over Array Elements
You can use a foreach
loop to iterate over the elements of an array. Here's an example:
foreach ($ages as $name => $age) {
echo "$name is $age years old.<br>";
}
This will output:
Alice is 25 years old.
Bob is 30 years old.
Charlie is 28 years old.
Array Functions
PHP provides several built-in functions to work with arrays. Some of the most commonly used ones are:
-
count()
: Returns the number of elements in an array. -
sort()
: Sorts the elements of an array in ascending order. -
rsort()
: Sorts the elements of an array in descending order. -
array_push()
: Adds one or more elements to the end of an array. -
array_pop()
: Removes the last element of an array. -
array_shift()
: Removes the first element of an array. -
array_unshift()
: Adds one or more elements to the beginning of an array.
Table of Array Functions
Function | Description |
---|---|
count() |
Returns the number of elements in an array |
sort() |
Sorts the elements of an array in ascending order |
rsort() |
Sorts the elements of an array in descending order |
array_push() |
Adds one or more elements to the end of an array |
array_pop() |
Removes the last element of an array |
array_shift() |
Removes the first element of an array |
array_unshift() |
Adds one or more elements to the beginning of an array |
I hope this tutorial helps you understand the basics of working with arrays in PHP. Remember, practice makes perfect, so try out these concepts and see how they work in real-world scenarios. Happy coding!
Credits: Image by storyset