Array Basics
Indexed Arrays
Section titled “Indexed Arrays”<?php// ============================================// PHP INDEXED ARRAY COMPREHENSIVE GUIDE// ============================================$fruits = array("Apple", "Banana", "Orange", "Mango");echo $fruits[0] . PHP_EOL ;// Output: AppleAssociative Arrays
Section titled “Associative Arrays”<?php// ============================================// PHP ASSOCIATIVE ARRAY EXAMPLE// ============================================
$person = [ "name" => "John Doe", "age" => 30, "city" => "New York"];
echo $person["name"] . PHP_EOL; // Output: John Doeecho $person["age"] . PHP_EOL; // Output: 30echo $person["city"] . PHP_EOL; // Output: New York
// Alternative syntax$car = array( "brand" => "Toyota", "model" => "Camry", "year" => 2024);
echo $car["brand"] . PHP_EOL; // Output: ToyotaMultidimensional Arrays
Section titled “Multidimensional Arrays”<?php// ============================================// PHP MULTIDIMENSIONAL ARRAY EXAMPLE// ============================================
// 2D Indexed Array$matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]];
echo $matrix[0][0] . PHP_EOL; // Output: 1echo $matrix[1][2] . PHP_EOL; // Output: 6
// Associative Multidimensional Array$students = [ "student1" => [ "name" => "Alice", "grade" => "A" ], "student2" => [ "name" => "Bob", "grade" => "B" ]];
echo $students["student1"]["name"] . PHP_EOL; // Output: Aliceecho $students["student2"]["grade"] . PHP_EOL; // Output: B
// Mixed Multidimensional Array$company = [ "IT" => [ ["name" => "John", "role" => "Developer"], ["name" => "Jane", "role" => "Designer"] ]];
echo $company["IT"][0]["name"] . PHP_EOL; // Output: Johnecho $company["IT"][1]["role"] . PHP_EOL; // Output: John