PHP Arrays

PHP Arrays – Complete Beginner Guide With Examples
Introduction to Arrays in PHP
In PHP, an array is a special data structure that allows you to store multiple values in a single variable.
Instead of creating separate variables like this:
You can store all values inside one array:
Arrays are extremely important in PHP because they are used in:
Storing lists of data
Working with databases
Handling form input
Managing configuration settings
Processing JSON data
Without arrays, handling large datasets would be difficult.
What Is an Array?
An array is a collection of values stored under a single variable name. Each value inside an array is called an element.
Each element has:
A key (index)
A value
Example:
Here:
0 → Red
1 → Green
2 → Blue
Types of Arrays in PHP
PHP supports three main types of arrays:
Indexed Arrays
Associative Arrays
Multidimensional Arrays
Let’s understand each one.
Indexed Arrays
Indexed arrays use numeric indexes (starting from 0).
Creating an Indexed Array
Method 1 (Short Syntax – Recommended)
Method 2 (Using array() function)
Accessing Array Elements
Looping Through Indexed Array
Associative Arrays
Associative arrays use named keys instead of numeric indexes.
Creating Associative Array
Accessing Values
Output:
Looping Through Associative Array
Multidimensional Arrays
A multidimensional array is an array inside another array.
Example:
Accessing Multidimensional Values
Associative Multidimensional Array
Counting Array Elements
Use count() function.
Complete PHP Code Example (count() Function)
Output:
Adding Elements to an Array
Add to Indexed Array
Add to Associative Array
Removing Elements from Array
unset()
array_pop() – Remove Last Element
array_shift() – Remove First Element
Sorting Arrays in PHP
PHP provides built-in sorting functions.
sort() – Sort Indexed Array
rsort() – Reverse Sort
asort() – Sort Associative Array by Value
ksort() – Sort by Key
Searching in Arrays
in_array()
array_search()
Merging Arrays
Converting Array to String
Use implode().
Converting String to Array
Use explode().
Real-World Example – Product Listing
Used in:
E-commerce websites
Blog posts
Admin dashboards
Common Beginner Mistakes
Forgetting Quotes in Associative Keys
Wrong:
Correct:
Accessing Wrong Index
Always check array length before accessing.
Mixing Indexed and Associative Styles Incorrectly
Keep structure consistent.
Best Practices for Using Arrays
- Use meaningful keys
- Validate array indexes
- Use foreach for arrays
- Use built-in functions
- Keep arrays structured
Why PHP Arrays Are Important
Arrays are used everywhere in PHP:
Database result sets
API responses
Form handling
Configuration files
Session data
Understanding arrays is essential before learning:
PHP loops
PHP forms
PHP database integration
JSON handling
Frequently Asked Questions (FAQs)
1. What is an array in PHP?
An array is a data structure that stores multiple values in a single variable.
2. How many types of arrays are there in PHP?
There are three types: Indexed arrays, Associative arrays, and Multidimensional arrays.
3. How do you loop through an array?
Using foreach or for loop.
4. How do you count array elements?
Using the count() function.
5. How do you add elements to an array?
Use square brackets [] or assign a new key-value pair.
Conclusion
PHP arrays are powerful tools for storing and managing multiple values efficiently. They are essential for real-world development and dynamic applications.
By mastering:
Indexed arrays
Associative arrays
Multidimensional arrays
Sorting
Searching
Looping
You can build structured and scalable PHP applications.
Arrays are one of the most important concepts in PHP programming.
