Saturday, September 9, 2017

PHP Arrays


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.

Or succinctly,
An array stores multiple values in one single variable.

Create an Array in PHP :
In PHP, the array() function is used to create an array.

Syntax :
array();

In PHP, there are three types of arrays:

  1. Indexed arrays - Arrays with a numeric index
  2. Associative arrays - Arrays with named keys
  3. Multidimensional arrays - Arrays containing one or more arrays


Example :
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "Best Car companies are " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Output:
---------------
Best Car companies are Volvo, BMW and Toyota.

Advantage of PHP Array:
1. Sorting: We can sort the elements of array easily.
2. Less Code: We don't need to define multiple variables.
3. Easy to traverse: By the help of single loop, we can traverse all the elements of an array.
4. Take Less Memory : Array allocate less memory.



No comments:

Post a Comment