codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

19 / 56 topics
19Arrays in PHP20Indexed Arrays21Associative Arrays22Multidimensional Arrays23Array Functions
Tutorials/PHP/Arrays in PHP
🐘PHP

Arrays in PHP

Updated 2026-05-15
10 min read

Arrays in PHP

Introduction

Arrays are fundamental data structures in PHP, allowing you to store multiple values in a single variable. They can be used to represent lists, collections of items, or even more complex data structures. In this tutorial, we'll explore how to create, manipulate, and work with arrays in PHP.

Concept

In PHP, an array is an ordered map that associates keys with values. Arrays can contain any type of value, including other arrays, making them highly versatile for various programming tasks. There are two main types of arrays in PHP:

  1. Indexed Arrays: These are arrays where the keys are integers starting from 0.
  2. Associative Arrays: These are arrays where the keys are strings or other non-integer values.

Examples

Creating Arrays

Indexed Arrays

To create an indexed array, you can use the array() function or the short array syntax [].

php
1$fruits = array("apple", "banana", "cherry");
2// Or using the short array syntax
3$fruits = ["apple", "banana", "cherry"];

Associative Arrays

Associative arrays use named keys that you assign to them.

php
1$person = array("name" => "John", "age" => 30, "city" => "New York");
2// Or using the short array syntax
3$person = ["name" => "John", "age" => 30, "city" => "New York"];

Accessing Array Elements

You can access elements of an array using their keys.

php
1echo $fruits[0]; // Outputs: apple
2echo $person["name"]; // Outputs: John

Modifying Arrays

You can modify existing arrays by adding, updating, or removing elements.

Adding Elements

To add an element to an indexed array, you can simply append it:

php
1$fruits[] = "orange";
2// Now $fruits contains ["apple", "banana", "cherry", "orange"]

For associative arrays, specify the key:

php
1$person["email"] = "john@example.com";
2// Now $person contains ["name" => "John", "age" => 30, "city" => "New York", "email" => "john@example.com"]

Updating Elements

To update an element, simply assign a new value to the existing key:

php
1$fruits[1] = "blueberry";
2// Now $fruits contains ["apple", "blueberry", "cherry", "orange"]
3$person["age"] = 31;
4// Now $person contains ["name" => "John", "age" => 31, "city" => "New York", "email" => "john@example.com"]

Removing Elements

To remove an element from an array, you can use the unset() function:

php
1unset($fruits[2]);
2// Now $fruits contains ["apple", "blueberry", "orange"]
3unset($person["email"]);
4// Now $person contains ["name" => "John", "age" => 31, "city" => "New York"]

Iterating Over Arrays

You can iterate over arrays using loops like foreach.

php
1foreach ($fruits as $fruit) {
2 echo $fruit . "<br>";
3}
4// Outputs:
5// apple
6// blueberry
7// orange
8
9foreach ($person as $key => $value) {
10 echo "$key: $value<br>";
11}
12// Outputs:
13// name: John
14// age: 31
15// city: New York

Array Functions

PHP provides a wide range of functions to work with arrays, such as count(), sort(), and array_push().

php
1echo count($fruits); // Outputs: 3
2sort($fruits);
3// Now $fruits contains ["apple", "blueberry", "orange"]
4array_push($fruits, "grape");
5// Now $fruits contains ["apple", "blueberry", "orange", "grape"]

What's Next?

In the next section, we will dive deeper into indexed arrays and explore more advanced techniques for working with them. Stay tuned!


PreviousRecursion in PHPNext Indexed Arrays

Recommended Gear

Recursion in PHPIndexed Arrays