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.

php Courses
php Courses
php Courses
PHP Array Basics: Creation, Access, Modification, Traversal, and Sorting

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: orange

Accessing 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: Beijing

Adding 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPData Structures
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.