PHP Arrays
Arrays in PHP behave a little differently than the arrays in C, as PHP is a dynamically typed language as against C which is a statically type language.PHP array is an ordered map (contains value on the basis of key). It is used to hold multiple values of similar type in a single variable.
<?php // One way to create an indexed array $name_one = array("Zack", "Anthony", "Ram", "Salim", "Raghav"); // Accessing the elements directly echo "Accessing the 1st array elements directly:\n"; echo $name_one[2], "\n"; echo $name_one[0], "\n"; echo $name_one[4], "\n"; // Second way to create an indexed array $name_two[0] = "ZACK"; $name_two[1] = "ANTHONY"; $name_two[2] = "RAM"; $name_two[3] = "SALIM"; $name_two[4] = "RAGHAV"; // Accessing the elements directly echo "Accessing the 2nd array elements directly:\n"; echo $name_two[2], "\n"; echo $name_two[0], "\n"; echo $name_two[4], "\n"; ?>
Types of Array in PHP
- Indexed or Numeric Arrays: An array with a numeric index where values are stored linearly.
- Associative Arrays: An array with a string index where instead of linear storage, each value can be assigned a specific key.
- Multidimensional Arrays: An array that contains a single or multiple arrays within it and can be accessed via multiple indices.