0% found this document useful (0 votes)
37 views4 pages

Working With Arrays in PHP

Uploaded by

imsisia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views4 pages

Working With Arrays in PHP

Uploaded by

imsisia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

WORKING WITH ARRAYS IN PHP

An array in PHP is a special variable that allows you to store multiple values in a single variable.
Arrays are a fundamental data structure and are highly versatile, enabling you to organize and
manipulate data efficiently.

1. Indexed Arrays

Definition

• Indexed arrays use numeric keys (starting from 0 by default) to access elements.
• They store a collection of values that are typically related but do not need specific labels.

Syntax

// Creating an indexed array


$array = array("Apple", "Banana", "Orange");

// Newer syntax (PHP 5.4+)


$array = ["Apple", "Banana", "Orange"];

Example

$fruits = ["Apple", "Banana", "Orange"];

// Accessing elements
echo $fruits[0]; // Outputs: Apple
echo $fruits[1]; // Outputs: Banana

// Adding new elements


$fruits[] = "Grapes"; // Adds "Grapes" to the array

// Iterating through the array


foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}

Real-World Application

• List of products in a store: e.g., ["Laptop", "Tablet", "Smartphone"]


• Days of the week: e.g., ["Monday", "Tuesday", "Wednesday"]

2. Associative Arrays

Definition

• Associative arrays use named keys instead of numeric indexes.


• These are perfect for associating related data, such as key-value pairs.

BASICS OF PHP - ARRAYS SOMATECH IT


Syntax

// Creating an associative array


$person = array(
"name" => "John",
"age" => 30,
"city" => "New York"
);

// Newer syntax
$person = [
"name" => "John",
"age" => 30,
"city" => "New York"
];

Example

$person = [
"name" => "Alice",
"age" => 28,
"city" => "Paris"
];

// Accessing elements
echo $person["name"]; // Outputs: Alice
echo $person["city"]; // Outputs: Paris

// Adding new key-value pairs


$person["country"] = "France";

// Iterating through the array


foreach ($person as $key => $value) {
echo "$key: $value<br>";
}

Real-World Application

• User profiles: e.g., ["username" => "john_doe", "email" =>


"[email protected]"]
• Configuration settings: e.g., ["theme" => "dark", "language" => "en"]

3. Multidimensional Arrays

Definition

• A multidimensional array is an array containing one or more arrays as its elements.


• These arrays are ideal for representing complex structures like tables or matrices.

Syntax
BASICS OF PHP - ARRAYS SOMATECH IT
// Creating a multidimensional array
$users = array(
array("John", 30, "New York"),
array("Alice", 25, "Paris"),
array("Bob", 35, "London")
);

Example

$users = [
["name" => "John", "age" => 30, "city" => "New York"],
["name" => "Alice", "age" => 25, "city" => "Paris"],
["name" => "Bob", "age" => 35, "city" => "London"]
];

// Accessing elements
echo $users[0]["name"]; // Outputs: John
echo $users[1]["city"]; // Outputs: Paris

// Iterating through the array


foreach ($users as $user) {
echo "Name: " . $user["name"] . ", Age: " . $user["age"] . ", City:
" . $user["city"] . "<br>";
}

Real-World Application

• Employee data: e.g., [[name, role, salary], [name, role, salary]]


• E-commerce products: e.g., [[name, price, category], [name, price,
category]]

Differences Between Array Types

Type Key Usage Example


Type
Indexed Array Numeric List-like collections ["Apple", "Banana"]
Associative Array Named Key-value pairs for better ["name" => "John",
"age" => 30]
context
Multidimensional Mixed Complex nested [[name, age], [name,
age]]
Array structures

Real-World Example: Student Grades

Problem

You are storing grades for three students in different subjects.

Indexed Array

BASICS OF PHP - ARRAYS SOMATECH IT


$grades = [85, 90, 78]; // Grades for one student

Associative Array

$grades = [
"Math" => 85,
"English" => 90,
"Science" => 78
];

Multidimensional Array

$students = [
["name" => "John", "grades" => ["Math" => 85, "English" => 90,
"Science" => 78]],
["name" => "Alice", "grades" => ["Math" => 88, "English" => 92,
"Science" => 84]],
];

Iterating Through the Multidimensional Array

foreach ($students as $student) {


echo "Name: " . $student["name"] . "<br>";
foreach ($student["grades"] as $subject => $grade) {
echo "$subject: $grade<br>";
}
echo "<br>";
}

Key Points to Remember

1. Arrays help organize data logically for efficient operations.


2. Use indexed arrays for simple lists, associative arrays for labeled data, and
multidimensional arrays for complex data structures.
3. PHP provides powerful tools like foreach to handle arrays easily.

Understanding and mastering arrays is crucial for building dynamic and robust PHP applications.

BASICS OF PHP - ARRAYS SOMATECH IT

You might also like