Backend Development 5 min read

PHP Arrays: Creation, Access, Manipulation, Traversal, and Common Functions

This article provides a comprehensive guide to PHP arrays, covering how to create them with array() or short syntax, initialize numeric arrays with range(), convert variables using compact(), and demonstrates accessing, adding, modifying, deleting, traversing with loops, and using built‑in functions, while also introducing related data structures.

php中文网 Courses
php中文网 Courses
php中文网 Courses
PHP Arrays: Creation, Access, Manipulation, Traversal, and Common Functions

PHP arrays are ordered maps used to store multiple values of various types, offering a powerful and flexible data structure for numbers, strings, objects, and even other arrays.

1. Creating and Initializing Arrays

1. Using array() function:

$fruits = array("apple", "banana", "orange");

2. Using short array syntax (PHP 5.4+):

$fruits = ["apple", "banana", "orange"];

3. Using range() function to create numeric arrays:

$numbers = range(1, 10); // creates an array containing 1 to 10

4. Using compact() function to convert variables into an array:

$name = "John";
$age = 30;
$user = compact("name", "age"); // creates an array containing $name and $age

2. Accessing and Manipulating Arrays

1. Accessing array elements:

You can use square brackets [] with the element's key.

echo $fruits[0]; // outputs "apple"

2. Adding elements:

You can use square brackets [] with a new key to add an element.

$fruits[] = "grape"; // adds "grape" at the end of the array

3. Modifying elements:

You can directly assign a new value to an existing key.

$fruits[1] = "pear"; // changes "banana" to "pear"

4. Deleting elements:

You can use the unset() function to remove an element.

unset($fruits[2]); // deletes "orange"

3. Traversing Arrays

1. Using foreach loop:

foreach ($fruits as $fruit) {
echo $fruit . " ";
}

2. Using for loop:

for ($i = 0; $i < count($fruits); $i++) {
echo $fruits[$i] . " ";
}

4. Common Array Functions

PHP provides many built‑in array functions for convenient operations:

count(): returns the number of elements.

sort(): sorts the array in ascending order.

rsort(): sorts the array in descending order.

array_merge(): merges two or more arrays.

array_search(): searches for a value and returns its key.

in_array(): checks if a value exists in the array.

5. Data Structures

Beyond arrays, PHP also supports other data structures such as:

Stack (LIFO)

Queue (FIFO)

Linked List

Tree

Graph

6. Summary

Arrays are a fundamental data structure in PHP; mastering them is essential for learning the language. This article covered creation, access, manipulation, traversal, and common functions, preparing you for more advanced topics like multidimensional arrays, sorting algorithms, and implementations of stacks and queues.

backend developmentprogrammingPHPData Structurescode examplesarrays
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

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