Comprehensive Guide on Arrays in PHP
1. Types of Arrays in PHP
PHP supports three main types of arrays:
1. Indexed Array: An array with a numeric index.
Example:
$colors = array("Red", "Green", "Blue");
echo $colors[1]; // Output: Green
2. Associative Array: An array where keys are named.
Example:
$student = array("name" => "Alice", "age" => 22, "city" => "Delhi");
echo $student["city"]; // Output: Delhi
3. Multidimensional Array: An array containing one or more arrays.
Example:
$students = array(
"Alice" => array("Math" => 85, "Science" => 90),
"Bob" => array("Math" => 78, "Science" => 88)
);
echo $students["Alice"]["Science"]; // Output: 90
2. Accessing Array Elements
You can access array elements using their index or key.
Indexed Array:
$colors = array("Red", "Green", "Blue");
echo $colors[0]; // Output: Red
Comprehensive Guide on Arrays in PHP
Associative Array:
$person = array("name" => "John", "age" => 25);
echo $person["name"]; // Output: John
Multidimensional Array:
$matrix = array(
array(1, 2, 3),
array(4, 5, 6)
);
echo $matrix[1][2]; // Output: 6
3. Performing Array Operations
Here are some common array operations in PHP:
Adding Elements:
$fruits = array("Apple", "Banana");
array_push($fruits, "Mango");
print_r($fruits); // Apple, Banana, Mango
Removing Elements:
array_pop($fruits);
print_r($fruits); // Apple, Banana
Merging Arrays:
$a = array(1, 2);
$b = array(3, 4);
$c = array_merge($a, $b);
print_r($c); // 1, 2, 3, 4
Comprehensive Guide on Arrays in PHP
Searching in Array:
$numbers = array(10, 20, 30);
echo in_array(20, $numbers) ? "Found" : "Not Found"; // Found
Sorting Arrays:
sort($numbers); // Ascending
rsort($numbers); // Descending
4. Taking Array Input from User
To take input from users, use HTML forms and PHP.
Example 1: Indexed Array Input
HTML:
<form method="post">
<input type="text" name="numbers[]">
<input type="text" name="numbers[]">
<input type="submit">
</form>
PHP:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$numbers = $_POST['numbers'];
foreach ($numbers as $value) {
echo $value . "<br>";
Example 2: Associative Array Input
HTML:
Comprehensive Guide on Arrays in PHP
<form method="post">
Name: <input type="text" name="student[name]"><br>
Age: <input type="text" name="student[age]"><br>
<input type="submit">
</form>
PHP:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$student = $_POST['student'];
echo "Name: " . $student["name"] . "<br>";
echo "Age: " . $student["age"];
5. Useful Array Functions
count($array) - Returns number of elements.
array_keys($array) - Returns all the keys.
array_values($array) - Returns all the values.
array_reverse($array) - Reverses the array.
array_search("value", $array) - Searches and returns key.
in_array("value", $array) - Checks if value exists.
Example:
$names = array("John", "Jane", "Jack");
echo count($names); // Output: 3
6. Best Practices
- Always check if variable is array using is_array().
- Use foreach for iterating through arrays.
Comprehensive Guide on Arrays in PHP
- Use descriptive keys in associative arrays.
- Validate and sanitize user input before processing.
- For large data, consider using loops with conditions.
Example:
if (is_array($data)) {
foreach ($data as $item) {
echo htmlspecialchars($item);