In the previous tutorial, you learned how PHP Functions help you organize and reuse code. Now imagine you need to store a list of ten student names, or a hundred product prices, inside your script. Creating a separate variable for each value would quickly become unmanageable. This is exactly the problem PHP Arrays solve.

This is Tutorial 7 of 10 in our PHP Basics for Beginners series. In this tutorial, you will learn how PHP Arrays work, the difference between indexed and associative arrays, and several essential array functions.

What are PHP Arrays?

PHP Arrays let you store multiple values inside a single variable. Instead of creating ten separate variables for ten student names, you can store all of them inside one array and access each value using its position or a custom key. PHP supports two main types of arrays: indexed arrays and associative arrays.

Indexed Arrays

An indexed array stores values in order, and PHP automatically assigns each value a numeric index starting from zero. Indexed PHP Arrays work well when you simply need an ordered list of items.

<?php
$fruits = array("Apple", "Banana", "Mango");

echo $fruits[0];
echo $fruits[1];
echo $fruits[2];
?>

Here, “Apple” sits at index 0, “Banana” at index 1, and “Mango” at index 2. You can also write PHP Arrays using the shorter square bracket syntax, which many developers prefer for its cleaner look.

<?php
$colors = ["Red", "Green", "Blue"];
echo $colors[1];
?>

Associative Arrays

Associative PHP Arrays let you assign custom, descriptive keys to each value instead of relying on plain numbers. This makes your code far easier to read when values need meaningful labels.

<?php
$student = array(
    "name" => "Ahmed",
    "age" => 22,
    "course" => "Web Development"
);

echo $student["name"];
echo $student["age"];
?>

Instead of remembering that index 0 holds a name and index 1 holds an age, associative PHP Arrays let you reference each value directly through a clear, descriptive key like “name” or “age”.

Multidimensional Arrays

PHP Arrays can also hold other arrays inside them, creating what is called a multidimensional array. This structure works well for representing more complex data, such as a list of multiple students with several details each.

<?php
$students = array(
    array("name" => "Ali", "age" => 20),
    array("name" => "Sara", "age" => 22)
);

echo $students[0]["name"];
echo $students[1]["age"];
?>

Looping Through Arrays

You already learned about the foreach loop in an earlier tutorial, and it pairs perfectly with PHP Arrays. It automatically visits every element without requiring you to track an index manually.

<?php
$fruits = array("Apple", "Banana", "Mango");

foreach ($fruits as $fruit) {
    echo $fruit . "<br>";
}
?>

For associative PHP Arrays, foreach can also grab both the key and the value at the same time, which makes displaying labeled data much simpler.

<?php
$student = array("name" => "Ahmed", "age" => 22);

foreach ($student as $key => $value) {
    echo $key . ": " . $value . "<br>";
}
?>

Common Array Functions

PHP includes many built-in functions that make working with PHP Arrays much easier. Here are some of the most useful ones you will use frequently.

Function Purpose
count() Counts the total number of elements in an array
array_push() Adds one or more elements to the end of an array
array_pop() Removes the last element from an array
sort() Sorts array values in ascending order
in_array() Checks whether a value exists inside an array
<?php
$numbers = array(5, 3, 8, 1);

echo count($numbers);

array_push($numbers, 10);
sort($numbers);

if (in_array(8, $numbers)) {
    echo "8 exists in the array.";
}
?>

These built-in functions save you from writing manual logic for common tasks, which becomes especially valuable as your PHP Arrays grow larger and more complex. For a complete list of array functions, check the official PHP array functions manual.

Adding and Removing Elements

You can add a new element to an indexed array simply by assigning a value to a new index, or by using array_push(). Removing elements works through functions like array_pop() for the last item, or unset() for a specific key.

<?php
$colors = ["Red", "Green", "Blue"];
$colors[] = "Yellow";

unset($colors[0]);

print_r($colors);
?>

The print_r() function displays the full structure of PHP Arrays, including all keys and values, which makes it an excellent debugging tool while you are still learning.

What’s Next?

You now understand how PHP Arrays work — indexed, associative, and multidimensional — along with looping techniques and essential array functions. In the next tutorial, we will explore PHP String Functions, which help you manipulate and format text data effectively.

If you missed the previous lesson, check out Tutorial 6: PHP Functions Guide, or explore all lessons in our PHP category.

Practice Exercise

Complete the following tasks to reinforce what you learned in this tutorial:

  • Create a PHP file named arrays-practice.php
  • Create an indexed array of five favorite books and print each one using a foreach loop
  • Create an associative array representing a product with keys for name, price, and quantity
  • Use foreach with key => value to print all details of the product array with labels
  • Create a multidimensional array holding three employees, each with a name and salary, then print all three names
  • Use count() to print the total number of employees in your multidimensional array
  • Use array_push() to add a new item to an indexed array, then use print_r() to display the updated array

Bonus Challenge: Create an indexed array of ten random numbers, use sort() to arrange them in ascending order, then use in_array() to check if the number 50 exists inside it and echo an appropriate message.

About Author

Arshad Sultan

Arshad Sultan is the Founder of Tutorials Ocean and a Technical Architect & Full-Stack Developer with 15+ years of experience in PHP, Laravel, CRM development, API integrations, and AI-powered automation. He also runs Rapid Code, a software and AI automation agency, and has trained hundreds of students across Karachi's IT institutes since 2010.

Leave a Reply

Your email address will not be published. Required fields are marked *