Working with Arrays
Create and manipulate arrays in PHP.
Working with Arrays is a free PHP Academy lesson on CoddyKit — lesson 5 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Working with Arrays
Welcome to another exciting lesson! In this lesson, you’ll learn how to use arrays in PHP to store and manage collections of data. Let’s dive in!

2
What Are Arrays?
An array is a data structure that allows you to store multiple values in a single variable. Each value is identified by an index.
Key Point: PHP arrays can store different types of data, including strings, integers, and even other arrays.
Example:
<?php
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[0]; // Outputs: Apple
?>
3
Types of Arrays
PHP supports three types of arrays:
- Indexed arrays: Use numeric indexes.
- Associative arrays: Use named keys.
- Multidimensional arrays: Contain arrays within arrays.
Let’s explore each type in detail.
4
Indexed Arrays
Indexed arrays use numeric indexes to store and access values. Example:
Tip: Use a for or foreach loop to iterate through indexed arrays.
<?php
$colors = ["Red", "Green", "Blue"];
echo $colors[1]; // Outputs: Green
?>
5
Associative Arrays
Associative arrays use named keys to store and access values. Example:
Task: Create an associative array for a product with keys like name, price, and stock.
<?php
$person = [
"name" => "Alice",
"age" => 25,
"city" => "New York"
];
echo $person["name"]; // Outputs: Alice
?>
6
Multidimensional Arrays
Multidimensional arrays contain arrays within arrays. Example:
Key Point: Use nested loops to iterate through multidimensional arrays.
<?php
$students = [
["name" => "Alice", "age" => 20],
["name" => "Bob", "age" => 22],
["name" => "Charlie", "age" => 18]
];
echo $students[0]["name"]; // Outputs: Alice
?>
7
Array Functions
PHP provides built-in functions to manipulate arrays. Examples:
count(): Get the number of elements.array_push(): Add an element to the end.array_pop(): Remove the last element.array_keys(): Get all keys.
Example:
<?php
$numbers = [1, 2, 3];
array_push($numbers, 4);
echo count($numbers); // Outputs: 4
?>
8
Iterating Through Arrays
Use loops to process all elements in an array. Example:
Tip: Use foreach for associative arrays to access both keys and values.
<?php
$fruits = ["Apple", "Banana", "Cherry"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>
9
<?php
$numbers = [10, 20, 30];
echo count($numbers);
?>
10
Great Job!
Congratulations! You’ve learned how to use arrays to store and manage collections of data in PHP. Arrays are a versatile tool for handling multiple values in a single variable. In the next lesson, we’ll explore functions in PHP. Let’s keep coding!

Frequently asked questions
Is the “Working with Arrays” lesson free?
Yes — the full text of “Working with Arrays” is free to read here on the web, and the PHP Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Working with Arrays”?
Create and manipulate arrays in PHP. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 5 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Working with Arrays” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Variables and Constants
- Data Types
- Operators
- Control Structures
- Working with Arrays