- Declaring Variables in PHP
- PHP Data Types
- PHP Arrays
- Types of PHP Operators
- PHP Strings
- PHP Expressions
- PHP Control Structures
- PHP Functions
- PHP Form Handling – Read Form Inputs & Handle File Uploads
- How to Connect PHP to MySQL Database Using MySQLi
- Executing Simple Queries in PHP
- Handling Results in PHP
- Handling Sessions and Cookies
PHP Arrays
PHP arrays are powerful tools that allow developers to store and manage multiple values in a single variable. This is especially useful when working with large amounts of related data such as names, items, or records.
Let’s imagine you’re running a car dealership and you want to manage the stock of your cars. Instead of creating individual variables for each car, PHP allows you to group them using arrays.
Definition: An array is a special variable that can hold more than one value at a time.
Types of PHP Arrays
PHP supports three types of arrays, each useful in different scenarios.
Indexed Arrays
These arrays use numeric indexes (starting from 0) to store data in a list format.
Example:
$cars = array("Volvo", "BMW", "Toyota");
echo $cars[0]; // Outputs: Volvo
You can also create indexed arrays manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
Associative Arrays
These use named keys to associate values, making the data more descriptive.
Example:
$age = array("Peter" => 35, "Ben" => 37, "Joe" => 43);
echo $age["Peter"]; // Outputs: 35
You can also declare it this way:
$age["Peter"] = 35;
$age["Ben"] = 37;
$age["Joe"] = 43;
Multidimensional Arrays
These are arrays that contain other arrays as elements. They are useful for representing tables or complex data.
Example:
$cars = array(
array("Volvo", 22, 18),
array("BMW", 15, 13),
array("Saab", 5, 2),
array("Land Rover", 17, 15)
);
echo $cars[0][0]; // Outputs: Volvo
echo $cars[0][1]; // Outputs: 22
Creating Arrays in PHP
There are two ways to create arrays:
Using the
array()
function:
$fruits = array("apple", "banana", "cherry");
Using the short array syntax (available since PHP 5.4):
$fruits = ["apple", "banana", "cherry"];
Accessing and Modifying Array Elements
To access values:
echo $fruits[1]; // Outputs: banana
echo $age["Joe"]; // Outputs: 43
To modify values:
$fruits[1] = "mango";
$age["Joe"] = 44;
Array Length
To get the number of elements in an array, use the count()
function.
Example:
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars); // Outputs: 3
Looping Through Arrays
Using for Loop (for indexed arrays):
$cars = array("Volvo", "BMW", "Toyota");
for ($i = 0; $i < count($cars); $i++) {
echo $cars[$i] . "\n";
}
Using foreach Loop (for all types):
$age = array("Peter" => 35, "Ben" => 37, "Joe" => 43);
foreach ($age as $name => $value) {
echo "Key=$name, Value=$value\n";
}
Working with Multidimensional Arrays
You can access nested elements using multiple indices.
Example:
$cars = array(
array("Volvo", 22, 18),
array("BMW", 15, 13)
);
echo $cars[0][0] . ": In stock: " . $cars[0][1] . ", sold: " . $cars[0][2];
Nested Loop Example:
$cars = array(
array("Volvo", 22, 18),
array("BMW", 15, 13),
array("Saab", 5, 2),
array("Land Rover", 17, 15)
);
for ($row = 0; $row < 4; $row++) {
echo "Row number $row\n";
for ($col = 0; $col < 3; $col++) {
echo $cars[$row][$col] . "\n";
}
}
Adding and Removing Elements
Adding values:
array_push($fruits, "orange");
$person["city"] = "Delhi";
Removing values:
array_pop($fruits); // Removes last element
unset($person["age"]); // Removes key-value pair
Useful Array Functions in PHP
array_merge()
: Combines arraysin_array()
: Checks if a value existssort()
: Sorts values in ascending order
Example:
$a = [1, 2];
$b = [3, 4];
$result = array_merge($a, $b);
print_r($result); // Outputs: [1, 2, 3, 4]
Conclusion
Arrays are essential for efficient data handling in PHP. Whether you’re creating a shopping cart, managing user data, or processing results, arrays give you flexibility and control. Learn to choose the right type of array and master the built-in functions—they’re your toolkit for smarter coding.
Â