PHP LogoArrays

Arrays are special variables that can store multiple values in a single variable. Instead of declaring separate variables for each item, you can store a collection of related data items under one name. Each item in an array is called an 'element', and each element has a unique identifier called an 'index' or 'key'.

PHP supports three main types of arrays:

1. Indexed Arrays (Numeric Arrays): Arrays with a numeric index. By default, the first element has an index of 0, the second 1, and so on. You can also manually assign numeric indices.
* Creation: `array("value1", "value2", ...)` or `["value1", "value2", ...]`
* Access: `$array[index]`

2. Associative Arrays: Arrays with named keys (strings) instead of standard numeric indices. These are ideal for storing data where each value has a specific name or label.
* Creation: `array("key1" => "value1", "key2" => "value2", ...)` or `["key1" => "value1", "key2" => "value2", ...]`
* Access: `$array['key']`

3. Multidimensional Arrays: Arrays containing one or more other arrays. This allows you to store data in a table-like structure, useful for representing complex data structures like matrices or lists of records.
* Creation: An array whose elements are themselves arrays.
* Access: `$array[index1][index2]` or `$array['key1']['key2']`

Key Characteristics of PHP Arrays:
* Dynamic Size: Arrays in PHP can grow or shrink in size as needed; you don't need to specify their size in advance.
* Mixed Data Types: A single array can store values of different data types (e.g., integers, strings, booleans, objects).
* Iteration: You can easily loop through array elements using constructs like `for`, `foreach`, or `while` loops.
* Rich Function Set: PHP provides a vast library of built-called functions for array manipulation (e.g., `count()`, `sort()`, `array_push()`, `array_pop()`, `unset()`).

Arrays are fundamental to almost any PHP application for organizing and managing collections of data efficiently.

Example Code

<?php

// 1. Indexed Array (Sayısal İndeksli Dizi)
echo "<h2>1. Indexed Array</h2>";

// Creating an indexed array
$fruits = ["Apple", "Banana", "Cherry"];

// Accessing elements
echo "First fruit: " . $fruits[0] . "<br>"; // Output: First fruit: Apple
echo "Second fruit: " . $fruits[1] . "<br>"; // Output: Second fruit: Banana

// Adding an element
$fruits[] = "Date"; // Adds to the next available index
echo "All fruits after adding: ";
print_r($fruits);
echo "<br>";

// Iterating through an indexed array
echo "<p>Iterating with for loop:</p>";
for ($i = 0; $i < count($fruits); $i++) {
    echo $fruits[$i] . "<br>";
}

// 2. Associative Array (İlişkisel Dizi)
echo "<h2>2. Associative Array</h2>";

// Creating an associative array
$person = [
    "name" => "Alice",
    "age" => 30,
    "city" => "New York"
];

// Accessing elements
echo "Person's name: " . $person["name"] . "<br>"; // Output: Person's name: Alice
echo "Person's age: " . $person["age"] . "<br>";   // Output: Person's age: 30

// Adding an element
$person["occupation"] = "Engineer";
echo "Person details after adding occupation: ";
print_r($person);
echo "<br>";

// Iterating through an associative array
echo "<p>Iterating with foreach loop:</p>";
foreach ($person as $key => $value) {
    echo ucfirst($key) . ": " . $value . "<br>";
}

// 3. Multidimensional Array (Çok Boyutlu Dizi)
echo "<h2>3. Multidimensional Array</h2>";

// Creating a multidimensional array (array of associative arrays)
$students = [
    ["name" => "Bob", "grade" => "A", "course" => "Math"],
    ["name" => "Charlie", "grade" => "B", "course" => "Science"],
    ["name" => "Diana", "grade" => "A-", "course" => "History"]
];

// Accessing elements
echo "Student 1 Name: " . $students[0]["name"] . "<br>";    // Output: Student 1 Name: Bob
echo "Student 2 Grade: " . $students[1]["grade"] . "<br>";  // Output: Student 2 Grade: B

// Iterating through a multidimensional array
echo "<p>All Students:</p>";
foreach ($students as $student) {
    echo "Name: " . $student["name"] . ", Grade: " . $student["grade"] . ", Course: " . $student["course"] . "<br>";
}

// Example of array functions
echo "<h2>Array Functions Example</h2>";
$numbers = [5, 2, 8, 1, 9];

echo "Original numbers: ";
print_r($numbers);
echo "<br>";

echo "Number of elements: " . count($numbers) . "<br>"; // Output: 5

sort($numbers); // Sorts the array in ascending order
echo "Sorted numbers: ";
print_r($numbers);
echo "<br>";

array_push($numbers, 10); // Add an element to the end
echo "Numbers after push: ";
print_r($numbers);
echo "<br>";

array_pop($numbers); // Remove the last element
echo "Numbers after pop: ";
print_r($numbers);
echo "<br>";

?>