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";<br/>$age = 30;<br/><br/>$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) {<br/>    echo $fruit . " ";<br/>}

2. Using for loop:

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

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.

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.

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

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.