Arrays are one of the most important data structures in PHP. They allow you to store and manage multiple values in a single variable. This lesson will cover the three types of arrays in PHP: Indexed, Associative, and Multidimensional arrays. Additionally, we will explore commonly used array functions.
7.1 Indexed Arrays
What is an Indexed Array?
- An indexed array stores a series of elements, each assigned an index starting from 0.
- Elements in an indexed array are accessed using numeric indices.
Creating an Indexed Array
- Using the
array()
function: - Using shorthand syntax:
Accessing Elements
Adding Elements
Looping Through an Indexed Array
7.2 Associative Arrays
What is an Associative Array?
- Associative arrays use named keys to access values instead of numeric indices.
Creating an Associative Array
Accessing Elements
Adding Elements
Looping Through an Associative Array
7.3 Multidimensional Arrays
What is a Multidimensional Array?
- Multidimensional arrays are arrays that contain other arrays as elements.
Creating a Multidimensional Array
Accessing Elements
Associative Multidimensional Array
Looping Through a Multidimensional Array
7.4 Array Functions
Commonly Used Array Functions
1. count()
- Returns the number of elements in an array.
- Example:
2. array_merge()
- Merges two or more arrays into one.
- Example:
3. array_push()
- Adds one or more elements to the end of an array.
- Example:
4. array_pop()
- Removes and returns the last element of an array.
- Example:
5. in_array()
- Checks if a value exists in an array.
- Example:
6. array_keys()
- Returns all the keys of an array.
- Example:
7. array_values()
- Returns all the values of an array.
- Example:
8. sort()
and rsort()
sort()
: Sorts an array in ascending order.rsort()
: Sorts an array in descending order.- Example:
9. array_reverse()
- Reverses the order of array elements.
- Example:
10. array_search()
- Searches for a value in an array and returns its key.
- Example:
Activities and Exercises
- Indexed Array:
- Create an array of your favorite movies and display each movie using a loop.
- Associative Array:
- Create an associative array representing a book (title, author, price) and display its details.
- Multidimensional Array:
- Create an array of students with their name, age, and city. Display all students using nested loops.
- Array Functions:
- Write a script to sort an array of numbers in descending order.
Assignment
Create a PHP script that:
- Stores details of products (name, price, category) in a multidimensional array.
- Displays all products using a loop.
- Sorts the products by price in ascending order.
- Searches for a product by name and displays its details.
Summary
In this lesson, you learned:
- The three types of arrays in PHP: Indexed, Associative, and Multidimensional.
- How to use key PHP array functions for managing arrays effectively.
Arrays are versatile and foundational in PHP, making them critical for handling complex data structures. Let me know if you need additional examples or exercises!
Leave a Reply