PHP Tutorial



ARRAYS IN PHP


๐Ÿ“š PHP Arrays

Arrays in PHP are used to store multiple values in a single variable. Instead of using separate variables for every value, arrays group them together efficiently.

Example: A list of student names can be stored in an array instead of separate variables.

๐Ÿ“Œ Why Use Arrays?

  • To store multiple values under a single name
  • To loop through values easily
  • To manage related data efficiently

๐Ÿงบ Types of Arrays

  • Indexed Array โ€“ uses numeric indexes
  • Associative Array โ€“ uses named keys
  • Multidimensional Array โ€“ contains arrays inside arrays

๐Ÿ”ข Indexed Array

<?php
$fruits = array("Apple", "Banana", "Mango");
echo $fruits[0]; // Apple
?>
  

๐Ÿ”  Associative Array

<?php
$age = array("John" => 25, "Jane" => 30);
echo $age["Jane"]; // 30
?>
  

๐Ÿ” Multidimensional Array

<?php
$students = array(
  array("John", 20),
  array("Jane", 22)
);
echo $students[1][0]; // Jane
?>
  

๐Ÿ”„ Looping Through Arrays

<?php
$colors = array("Red", "Green", "Blue");
foreach ($colors as $color) {
  echo $color . "<br>";
}
?>
  
Tip: Arrays are the backbone of dynamic data storage in PHP โ€” learn them well!

๐Ÿ› ๏ธ Array Functions (Quick List)

  • count() โ€“ Get number of elements
  • array_push() โ€“ Add elements
  • array_merge() โ€“ Merge two arrays
  • array_keys() โ€“ Get keys
  • in_array() โ€“ Check if value exists

๐Ÿ“Œ Summary

  • Arrays store multiple values in one variable
  • They can be indexed, associative, or multidimensional
  • Easy to loop through and manipulate with array functions

Practice Idea: Try making a list of students with names and grades using associative arrays.


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review