PHP Array
PHP arrays are flexible and powerful data structures that allow you to store multiple values in a single variable. They can hold different types of values such as integers, strings, or even other arrays. There are three main types of arrays in PHP:
- Indexed Arrays: Arrays with a numeric index.
- Associative Arrays: Arrays where the keys are named.
- Multidimensional Arrays: Arrays containing one or more arrays.
Let's explore each type with examples and outputs.
Indexed Arrays
Indexed arrays use numeric indices to access their values.
<?php
$fruits = array("Apple", "Banana", "Cherry");
// Accessing values
echo $fruits[0]; // Output: Apple
echo $fruits[1]; // Output: Banana
echo $fruits[2]; // Output: Cherry
// Adding values
$fruits[] = "Date"; // Adds "Date" at the end of the array
// Looping through the array
foreach($fruits as $fruit) {
echo $fruit . " ";
}
// Output: Apple Banana Cherry Date
?>
Associative Arrays
Associative arrays use named keys to access their values.
<?php
$ages = array("Peter" => 35, "Ben" => 37, "Joe" => 43);
// Accessing values
echo $ages["Peter"]; // Output: 35
echo $ages["Ben"]; // Output: 37
echo $ages["Joe"]; // Output: 43
// Adding values
$ages["Anna"] = 25; // Adds "Anna" => 25 to the array
// Looping through the array
foreach($ages as $name => $age) {
echo "$name is $age years old. ";
}
// Output: Peter is 35 years old. Ben is 37 years old. Joe is 43 years old. Anna is 25 years old.
?>
Multidimensional Arrays
Multidimensional arrays contain one or more arrays within them.
<?php
$students = array(
"John" => array("Math" => 85, "Science" => 78, "English" => 90),
"Jane" => array("Math" => 92, "Science" => 88, "English" => 84),
"Doe" => array("Math" => 70, "Science" => 75, "English" => 80)
);
// Accessing values
echo $students["John"]["Math"]; // Output: 85
echo $students["Jane"]["Science"]; // Output: 88
echo $students["Doe"]["English"]; // Output: 80
// Looping through the array
foreach($students as $name => $subjects) {
echo "$name's scores: ";
foreach($subjects as $subject => $score) {
echo "$subject: $score ";
}
echo "\n";
}
// Output:
// John's scores: Math: 85 Science: 78 English: 90
// Jane's scores: Math: 92 Science: 88 English: 84
// Doe's scores: Math: 70 Science: 75 English: 80
?>
Functions for Arrays
PHP provides a wide range of functions to manipulate arrays. Here are a few commonly used ones:
count($array)
: Returns the number of elements in an array.array_merge($array1, $array2)
: Merges two or more arrays.array_keys($array)
: Returns all the keys of an array.array_values($array)
: Returns all the values of an array.in_array($value, $array)
: Checks if a value exists in an array.
Example
<?php
$colors = array("Red", "Green", "Blue");
// Count elements
echo count($colors); // Output: 3
// Merge arrays
$moreColors = array("Yellow", "Purple");
$allColors = array_merge($colors, $moreColors);
// Print merged array
foreach($allColors as $color) {
echo $color . " ";
}
// Output: Red Green Blue Yellow Purple
// Check if a value exists
if(in_array("Green", $allColors)) {
echo "Green is in the array.";
} else {
echo "Green is not in the array.";
}
// Output: Green is in the array.
?>
These examples cover the basics of PHP arrays. They are fundamental for managing and manipulating collections of data in PHP.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.