PHP arrays are a convenient data structure that allows you to manage multiple pieces of data together. In web applications, arrays are useful in many situations such as passing form data or storing configuration information. This article explains arrays in an easy-to-understand way for beginners, from the basics to multidimensional arrays and practical usage examples, all with code.
What is a PHP Array? Basic Concepts and Types
PHP arrays come in two types: ordered “indexed arrays” and key-value paired “associative arrays.” Both are treated as array
type and can store any data type. For example, they can hold numbers, strings, objects, or even other arrays as elements. Using arrays makes data management more efficient without defining many variables.
- Indexed arrays
Arrays with elements numbered sequentially, accessed by numeric keys. - Associative arrays
Arrays managed as key-value pairs, accessed by string or numeric keys.
How to Create Arrays: Indexed and Associative Arrays
<?php
// Creating an indexed array
$fruits = ['apple', 'banana', 'orange'];
// Display each element of the indexed array
echo $fruits[0]; // apple
echo $fruits[1]; // banana
echo $fruits[2]; // orange
// Creating an associative array
$user = [
'name' => 'Taro Yamada',
'age' => 30,
'email'=> 'taro@example.com'
];
// Display elements of the associative array by key
echo $user['name']; // Taro Yamada
echo $user['age']; // 30
echo $user['email']; // taro@example.com
?>
Indexed arrays are created simply by listing elements inside []
, while associative arrays are defined with key => value
pairs. Both can also be created using the array()
function.
Accessing and Manipulating Array Elements (Add, Update, Delete)
In PHP arrays, you can easily access, add, update, or delete elements using indexes or keys.
Below are basic operation examples.
<?php
// Example of indexed array
$fruits = ['apple', 'banana', 'orange'];
// Example of associative array
$user = [
'name' => 'Taro Yamada',
'age' => 30,
'email'=> 'taro@example.com'
];
// Adding element (appending to indexed array)
$fruits[] = 'grape'; // $fruits becomes ['apple', 'banana', 'orange', 'grape']
// Updating element (change value of key 'age' in associative array)
$user['age'] = 31; // $user['age'] is updated to 31
// Deleting element (remove 0th element from indexed array)
unset($fruits[0]); // 'apple' is deleted; note indexes are not reindexed automatically
?>
Details of Each Operation
Addition
For indexed arrays, you can add a new element at the end with $array[] = value;
.
For associative arrays, add elements by assigning a value to a new key.
Example: $user['address'] = 'Tokyo';
Update
You overwrite values by assigning to existing keys or indexes.
Deletion
You can delete specific elements using the unset()
function. However, for indexed arrays, the indexes are not automatically reindexed after deletion.
If you want to maintain continuous indexes, you need to rebuild the array using array_values()
.
<?php
// Example of reindexing after deletion
unset($fruits[0]);
$fruits = array_values($fruits); // Indexes are reset starting from 0
?>
Frequently Used Array Functions: array_push, array_merge, array_filter, etc.
<?php
// Adding multiple elements with array_push (append at the end)
$fruits = ['apple', 'banana'];
array_push($fruits, 'melon', 'pineapple');
// $fruits becomes ['apple', 'banana', 'melon', 'pineapple']
// Combining arrays with array_merge
$colors1 = ['red', 'blue'];
$colors2 = ['green', 'yellow'];
$allColors = array_merge($colors1, $colors2);
// $allColors becomes ['red', 'blue', 'green', 'yellow']
// Filtering elements with array_filter (extract even numbers)
$numbers = [1, 2, 3, 4, 5];
$even = array_filter($numbers, fn($n) => $n % 2 === 0);
// $even becomes [1 => 2, 3 => 4] (original keys are preserved)
?>
array_push
appends elements, array_merge
joins arrays, and array_filter
extracts elements by callback condition. There are many other useful functions like array_map
and array_slice
.
What is a Multidimensional Array? Basics and Usage
A multidimensional array is an array that contains arrays as elements.
Using multidimensional arrays allows you to handle tabular data or complex structured data.
Example of a Multidimensional Array
<?php
// 2-dimensional indexed array (array of arrays)
$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Get the element at 2nd row (index 1), 3rd column (index 2)
echo $matrix[1][2]; // Output: 6
// Multidimensional array containing associative arrays (list of user info)
$users = [
[
'name' => 'Taro Yamada',
'age' => 30,
'email'=> 'taro@example.com'
],
[
'name' => 'Hanako Sato',
'age' => 25,
'email'=> 'hanako@example.com'
]
];
// Get the name of the first user (index 0)
echo $users[0]['name']; // Output: Taro Yamada
?>
Advantages of Multidimensional Arrays
- Manage complex data structures in a single variable
- Easy to organize tabular data, lists of lists, or configuration info
- Flexible manipulation using nested loops and functions
Multidimensional arrays are one of PHP’s fundamental data structures and are very useful for data management in web development.
Beginners should get used to thinking about array hierarchy step-by-step while practicing.
How to Check What Data is Stored in an Array
- For a quick content check: use
print_r()
+<pre>
- To check detailed type info: use
var_dump()
+<pre>
- To output as PHP code: use
var_export()
+<pre>
Purpose | Function to Use | Description |
---|---|---|
Quickly check contents | print_r() | Displays contents in a readable format without types |
Check with types and details | var_dump() | Displays types, counts, and other details |
Use contents as PHP code | var_export() | Displays contents as PHP array code |
1. Using print_r()
<?php
$array = [
"fruit" => "apple",
"count" => 3,
"colors" => ["red", "green", "yellow"]
];
echo '<pre>';
print_r($array);
echo '</pre>';
?>
Output Example

2. Using var_dump()
Use this when you want to see types and detailed info.
<?php
echo '<pre>';
var_dump($array);
echo '</pre>';
?>
Output Example

3. Using var_export()
Useful for displaying the array structure as PHP code.
<?php
echo '<pre>';
var_export($array);
echo '</pre>';
?>
Output Example

Summary
This article explained the basics of PHP arrays including creation, manipulation, useful functions, multidimensional arrays, and methods to check array contents. Mastering arrays improves data management efficiency and leads to maintainable code. Start practicing to get comfortable with arrays and apply them in various scenarios.