PHP Array Basics: Creation, Access, Modification, Traversal, and Sorting
This tutorial explains PHP arrays, covering their basic concepts, how to create indexed and associative arrays, access elements, add and remove items, traverse using loops, and sort arrays, with clear code examples for each operation.
Arrays are a fundamental data structure in PHP that allow storage of multiple values of any type, including strings, integers, and floats. PHP supports both indexed (numeric) arrays and associative (key‑value) arrays.
Array Basic Concept
An array is a special variable that can hold multiple values, which may be accessed via numeric indexes or custom keys.
Creating and Initializing Arrays
Creating Indexed Arrays
Indexed arrays are ordered lists where each element has a unique numeric index. They can be created using the array() function or the short [] syntax.
// Using array() function
$fruits = array("apple", "banana", "orange");
// Using short syntax
$fruits = ["apple", "banana", "orange"];Creating Associative Arrays
Associative arrays store key‑value pairs, where each element is identified by a custom key.
// Using array() function
$person = array(
"name" => "Tom",
"age" => 25,
"city" => "Beijing"
);
// Using short syntax
$person = [
"name" => "Tom",
"age" => 25,
"city" => "Beijing"
];Accessing Array Elements
Accessing Indexed Array Elements
Elements are accessed by their numeric index, starting from 0.
$fruits = ["apple", "banana", "orange"];
echo $fruits[0]; // outputs: apple
echo $fruits[1]; // outputs: banana
echo $fruits[2]; // outputs: orangeAccessing Associative Array Elements
Elements are accessed by their custom keys.
$person = ["name" => "Tom", "age" => 25, "city" => "Beijing"];
echo $person["name"]; // outputs: Tom
echo $person["age"]; // outputs: 25
echo $person["city"]; // outputs: BeijingAdding and Deleting Array Elements
Adding Elements to Indexed Arrays
New items can be appended or inserted at a specific index.
$fruits = ["apple", "banana", "orange"];
$fruits[] = "grape"; // append to the end
$fruits[1] = "pear"; // replace element at index 1
print_r($fruits);Adding Elements to Associative Arrays
New key‑value pairs are added by assigning a value to a new key.
$person = ["name" => "Tom", "age" => 25];
$person["city"] = "Beijing"; // add new pair
print_r($person);Deleting Elements
The unset() function removes elements from both indexed and associative arrays.
$fruits = ["apple", "banana", "orange"];
unset($fruits[1]); // remove element at index 1
print_r($fruits);
$person = ["name" => "Tom", "age" => 25, "city" => "Beijing"];
unset($person["age"]); // remove element with key "age"
print_r($person);Array Traversal and Sorting
Traversing Indexed Arrays
Both for loops and foreach loops can iterate over indexed arrays.
$fruits = ["apple", "banana", "orange"];
// for loop
for ($i = 0; $i < count($fruits); $i++) {
echo $fruits[$i] . " ";
}
// foreach loop
foreach ($fruits as $fruit) {
echo $fruit . " ";
}Traversing Associative Arrays
Use foreach to iterate over key‑value pairs.
$person = ["name" => "Tom", "age" => 25, "city" => "Beijing"];
foreach ($person as $key => $value) {
echo $key . ": " . $value . " ";
}Sorting Arrays
The sort() function orders an indexed array in ascending order, while rsort() orders it in descending order.
$numbers = [3, 1, 2];
sort($numbers); // ascending
print_r($numbers);
rsort($numbers); // descending
print_r($numbers);This article introduced the fundamental concepts of PHP arrays, demonstrated how to create, access, modify, traverse, and sort them, and provided concrete code examples to help readers apply arrays effectively in PHP programming.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
