PHP Array

PHP array is a data structure that contains one or more related types of values in a single value.

For example if you want to store 500 names then instead of defining different variable for each name its easy to define an array of 500 length.

Multiple values can be stored in an array within a single variable , and the values can be accessed by referring to an index number.

PHP Array Synatx:

To make an array in PHP, array() function is used:

array();
<h2> Example of Array</h2>
<?php  
$lang= array("C","C++","Java");  
  echo "Programming Languages are " . $lang[0] . ", " . $lang[1] . " and " . $lang[2] . ".";
?>

Types of PHP Array:

There are three types of array in PHP:

TypeDescription
Numeric arraysArrays having numeric index
Associative arraysArrays having named index
Multidimensional arraysArrays containing one or more arrays

These types will be discussed in detail in upcoming chapters.

Advantage of PHP Array

  • Less code: Multiple values under one variable can be saved.
  • Easy to Remember: There is no need to remember multiple variable names.
  • Less memory utilization: Memory is allocated to a single array, and multiple values can be saved to save memory.
  • Easy to traverse: We can explore all the items of an array using a single loop.
  • Sorting: The array elements can be sorted easily.