Working with Arrays in PHP:
In PHP, arrays are versatile data structures that allow you to store multiple values of different types under a single variable name. Arrays can be indexed numerically or associatively, and they can also be multidimensional, meaning they can contain other arrays as elements. Let's explore how to work with arrays in PHP with examples.
Indexed Arrays:
Indexed arrays in PHP store elements with numerical indexes, starting from zero. You can access elements by their index or iterate over the array using loops.
// Creating an indexed array
$colors = array("Red", "Green", "Blue");
// Accessing elements by index
echo $colors[0]; // Output: Red
// Iterating over the array
foreach ($colors as $color) {
echo $color . " ";
}
// Output: Red Green Blue
?>
In this example, $colors
is an indexed array containing three elements. Elements are accessed using their numerical indexes, starting from 0. The foreach
loop iterates over the array, printing each color.
Associative Arrays:
Associative arrays in PHP use key-value pairs, where each element is associated with a specific key. Keys can be strings or integers, and they provide a named index for accessing elements.
// Creating an associative array
$person = array("name" => "John", "age" => 30, "city" => "New York");
// Accessing elements by key
echo $person["name"]; // Output: John
// Iterating over the array
foreach ($person as $key => $value) {
echo "$key: $value ";
}
// Output: name: John age: 30 city: New York
?>
In this example, $person
is an associative array with keys "name"
, "age"
, and "city"
, each associated with corresponding values. Elements are accessed using their keys, and the foreach
loop iterates over the array, printing key-value pairs.
Multidimensional Arrays:
Multidimensional arrays in PHP contain other arrays as elements. They can be used to represent more complex data structures, such as matrices or records with multiple fields.
// Creating a multidimensional array
$matrix = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
// Accessing elements by indices
echo $matrix[0][1]; // Output: 2
// Iterating over the array
foreach ($matrix as $row) {
foreach ($row as $element) {
echo $element . " ";
}
echo "<br>";
}
// Output:
// 1 2 3
// 4 5 6
// 7 8 9
?>
In this example, $matrix
is a multidimensional array representing a 3x3 matrix. Elements are accessed using two sets of indices: the row index and the column index. Nested foreach
loops iterate over the array, printing each element.
Practical Example:
Let's create an associative array representing a person's details and a multidimensional array representing a list of books with their authors and prices.
// Associative array representing person's details
$person = array(
"name" => "Alice",
"age" => 25,
"city" => "London"
);
// Multidimensional array representing books
$books = array(
array("title" => "The Great Gatsby", "author" => "F. Scott Fitzgerald", "price" => 10.99),
array("title" => "To Kill a Mockingbird", "author" => "Harper Lee", "price" => 12.50),
array("title" => "1984", "author" => "George Orwell", "price" => 8.95)
);
// Accessing elements
echo "Name: " . $person["name"] . "<br>";
echo "Age: " . $person["age"] . "<br>";
echo "City: " . $person["city"] . "<br>";
foreach ($books as $book) {
echo "Title: " . $book["title"] . ", ";
echo "Author: " . $book["author"] . ", ";
echo "Price: $" . $book["price"] . "<br>";
}
?>
In this example,
$person
is an associative array representing a person's details, and $books
is a multidimensional array representing a list of books. Elements are accessed using keys or indices, and the foreach
loop iterates over the arrays, printing the details. Practice Excercise Practice now