Arrays: Free PHP & MySQL Tutorial
Master PHP Arrays with Hands-On Coding Examples
Array Types You'll Master
Simple Arrays
Basic indexed arrays using numerical positions. Start with zero-based indexing for organizing sequential data like lists.
Associative Arrays
Key-value pair arrays using meaningful string identifiers. Perfect for structured data like customer information.
Multidimensional Arrays
Nested arrays containing other arrays. Essential for complex data structures like database records or product catalogs.
Arrays are one of the most common and powerful variable types in PHP, allowing you to group related information together efficiently.
Creating Your First Associative Array
Define Array Elements
Create variables using $customer['key'] syntax with string keys like 'firstName', 'lastName', 'city', and 'state'.
Output Array Values
Use echo with concatenation (period operator) to combine strings with array values for proper display.
Test in Browser
Navigate to localhost to verify your array outputs correctly in the web browser.
Associative vs Indexed Arrays
| Feature | Associative Arrays | Indexed Arrays |
|---|---|---|
| Key Type | String keys ('firstName') | Numeric keys (0, 1, 2) |
| Access Method | $customer['firstName'] | $cars[0] |
| Best Use Case | Structured data | Sequential lists |
| Echo Syntax | Requires concatenation | Can embed directly |
Below your commented echo line, implement the modern array syntax:
$restaurant = [ 'name' => 'Nobu', 'type' => 'Sushi', 'price' => 'Expensive' ];This concise syntax creates an associative array with key-value pairs. The element before the => operator is the key, while the element after is its corresponding value. This structure mirrors real-world data relationships and is the foundation for working with JSON, APIs, and database results.
Demonstrate array value access and concatenation:
$restaurant = [ 'name' => 'Nobu', 'type' => 'Sushi', 'price' => 'Expensive' ]; echo $restaurant['name']. ' is very '.$restaurant['price'];This example shows how to combine multiple array values with string literals using concatenation, a technique you'll frequently use when building dynamic content or user interfaces.
Save and test your code:
- Mac: localhost:8888/phpclass/array.php
- Windows: localhost/phpclass/array.php
Expected output: Nobu is very Expensive
Shorthand Array Syntax Evolution
| Feature | Old Syntax (Pre-5.4) | New Syntax (5.4+) |
|---|---|---|
| Declaration | array() | [ ] |
| Key-Value Pairs | array('key' => 'value') | ['key' => 'value'] |
| Readability | More verbose | Cleaner, simpler |
Building Multidimensional Arrays
Create Empty Container
Start with an empty array using $restaurant = [ ]; to prepare for nested structures.
Add First Nested Array
Insert a complete associative array as the first element with restaurant details like name, type, and price.
Add Additional Elements
Separate multiple nested arrays with commas to create a collection of related data structures.
Access Nested Data
Use double bracket notation like $restaurant[0]['name'] to access specific values in nested arrays.
Remember that PHP arrays start at index 0. The first element is [0], the second is [1], and so on.
Using print_r() for Debugging
Array Best Practices Checklist
Makes code self-documenting and easier to maintain
Prevents off-by-one errors when accessing elements
Ensures arrays display correctly when combined with strings
Required syntax for valid PHP array structure
Quickly visualize entire array contents and structure
Key Takeaways