Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to Arrays in PHP

What is an Array?

An array in PHP is a data structure that allows you to store multiple values in a single variable. Arrays are useful for organizing data and can be used to store a collection of data items of the same type or different types. Arrays can be indexed or associative.

Types of Arrays

There are three types of arrays in PHP:

  • Indexed Arrays: Arrays with a numeric index.
  • Associative Arrays: Arrays with named keys.
  • Multidimensional Arrays: Arrays containing one or more arrays.

Creating Indexed Arrays

Indexed arrays use numeric indices starting from 0. Here's how you can create an indexed array:

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

You can also use the short array syntax:

<?php
$fruits = ["Apple", "Banana", "Orange"];
?>
                

Accessing Indexed Arrays

You can access elements in an indexed array using their index:

<?php
echo $fruits[0]; // Outputs: Apple
echo $fruits[1]; // Outputs: Banana
echo $fruits[2]; // Outputs: Orange
?>
                
Apple Banana Orange

Creating Associative Arrays

Associative arrays use named keys that you assign to them. Here's how to create an associative array:

<?php
$ages = array("Peter" => 35, "Ben" => 37, "Joe" => 43);
?>
                

You can also use the short array syntax:

<?php
$ages = ["Peter" => 35, "Ben" => 37, "Joe" => 43];
?>
                

Accessing Associative Arrays

You can access elements in an associative array using their keys:

<?php
echo $ages["Peter"]; // Outputs: 35
echo $ages["Ben"];   // Outputs: 37
echo $ages["Joe"];   // Outputs: 43
?>
                
35 37 43

Multidimensional Arrays

Multidimensional arrays contain one or more arrays. Here's an example of a two-dimensional array:

<?php
$contacts = array(
    array("Peter", "peter@example.com"),
    array("Ben", "ben@example.com"),
    array("Joe", "joe@example.com")
);
?>
                

Accessing Multidimensional Arrays

You can access elements in a multidimensional array using multiple indices:

<?php
echo $contacts[0][0]; // Outputs: Peter
echo $contacts[1][1]; // Outputs: ben@example.com
?>
                
Peter ben@example.com

Looping Through Arrays

You can loop through arrays using foreach loops. Here’s an example for an indexed array:

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

And here’s an example for an associative array:

<?php
foreach ($ages as $name => $age) {
    echo "$name is $age years old.<br>";
}
?>
                
Peter is 35 years old. Ben is 37 years old. Joe is 43 years old.

Conclusion

Arrays are a fundamental part of PHP and are essential for storing and managing data. This introduction covered the basics of creating, accessing, and looping through different types of arrays. With this knowledge, you can start using arrays to manage collections of data in your PHP applications.