EZ

Eduzan

Learning Hub

Eduzan
Eduzan / PHP

Arrays in PHP

Arrays in PHP are data structures that allow us to store multiple values under a single variable, whether they are of similar or different data types. This makes it easier to manage large sets of data without needing to create separate variables for each value. Arrays are particularly useful when handling lists, and they provide an easy way to access elements using an index or key.

Types of Arrays in PHP

1. Indexed (or Numeric) Arrays: Arrays that use numeric indices for storing and accessing values.
2. Associative Arrays: Arrays where each value is associated with a user-defined string key, instead of a numeric index.
3. Multidimensional Arrays: Arrays that can contain other arrays within them, making it possible to store complex data structures.

1. Indexed or Numeric Arrays: These arrays use numbers as keys, starting at zero by default. Values can be stored and accessed using these indices. Below are examples to illustrate this concept.

Example 1:

<?php

// Defining an indexed array
$fruits = array("Apple", "Banana", "Cherry", "Mango", "Orange");

// Accessing elements directly
echo "First array elements:\n";
echo $fruits[2], "\n"; // Outputs: Cherry
echo $fruits[0], "\n"; // Outputs: Apple
echo $fruits[4], "\n"; // Outputs: Orange

// Another way of creating an indexed array
$vegetables[0] = "Carrot";
$vegetables[1] = "Broccoli";
$vegetables[2] = "Spinach";
$vegetables[3] = "Tomato";
$vegetables[4] = "Pepper";

// Accessing elements from second array
echo "Second array elements:\n";
echo $vegetables[2], "\n"; // Outputs: Spinach
echo $vegetables[0], "\n"; // Outputs: Carrot
echo $vegetables[4], "\n"; // Outputs: Pepper

?>

Output:

First array elements:
Cherry
Apple
Orange
Second array elements:
Spinach
Carrot
Pepper

Example 2: Traversing Indexed Arrays Using Loops

<?php

// Creating an indexed array
$colors = array("Red", "Green", "Blue", "Yellow", "Purple");

// Looping through array using foreach
echo "Looping with foreach:\n";
foreach ($colors as $color) {
    echo $color . "\n";
}

// Counting elements in array
$count = count($colors);
echo "\nTotal elements: $count \n";

// Looping through array using for loop
echo "Looping with for loop:\n";
for ($i = 0; $i < $count; $i++) {
    echo $colors[$i] . "\n";
}

?>

Output:

Looping with foreach:
Red
Green
Blue
Yellow
Purple
Total elements: 5
Looping with for loop:
Red
Green
Blue
Yellow
Purple

2. Associative Arrays: Associative arrays use named keys instead of numeric indices, making it possible to create more meaningful associations between keys and values. Below are examples of associative arrays.

Example 1:

<?php

// Creating an associative array
$people = array("John"=>"Manager", "Sara"=>"Developer", "Mike"=>"Designer");

// Accessing elements directly
echo "Job of John: " . $people["John"] . "\n"; // Outputs: Manager
echo "Job of Sara: " . $people["Sara"] . "\n"; // Outputs: Developer
echo "Job of Mike: " . $people["Mike"] . "\n"; // Outputs: Designer

?>

Output:

Job of John: Manager
Job of Sara: Developer
Job of Mike: Designer

Example 2: Traversing Associative Arrays

<?php

// Creating an associative array
$employee_roles = array(
    "Alice" => "Project Manager",
    "Bob" => "Lead Developer",
    "Charlie" => "UX Designer"
);

// Looping through associative array using foreach
echo "Looping with foreach:\n";
foreach ($employee_roles as $name => $role) {
    echo "$name's role is $role\n";
}

// Looping using array_keys and for loop
echo "\nLooping with for loop:\n";
$keys = array_keys($employee_roles);
$length = count($employee_roles);

for ($i = 0; $i < $length; $i++) {
    echo $keys[$i] . " is a " . $employee_roles[$keys[$i]] . "\n";
}

?>

Output:

Looping with foreach:
Alice's role is Project Manager
Bob's role is Lead Developer
Charlie's role is UX Designer

Looping with for loop:
Alice is a Project Manager
Bob is a Lead Developer
Charlie's role is UX Designer

3. Multidimensional Arrays: These arrays store other arrays within each element, which can be accessed using multiple indices. Below is an example of how to work with multidimensional arrays.

Example 1:

<?php

// Defining a multidimensional array
$students = array(
    array("name" => "John", "age" => 20, "grade" => "A"),
    array("name" => "Jane", "age" => 22, "grade" => "B"),
    array("name" => "Tom", "age" => 21, "grade" => "A")
);

// Accessing elements
echo $students[0]["name"] . " has grade: " . $students[0]["grade"] . "\n";
echo $students[2]["name"] . " is " . $students[2]["age"] . " years old\n";

?>

Output:

John has grade: A
Tom is 21 years old
End of lesson.