PHP Arrays : Basics



PHP Array :

  1. An array is a data structure which is called as collection of similar type of data.
  2. Suppose we need to store roll numbers of 100 students then we can use array variable roll which can store 100 values in single variable with index.

PHP array

Create an Array in PHP :

The array() function is used to create an array:

array();

Consider following example –

$shapes = array("square","rectangle","circle","oval");

The above array will be like this –

Array Element Value of Array Element
shapes[0] square
shapes[1] rectangle
shapes[2] circle
shapes[3] oval

PHP Size of Array :

<?php
$shapes = array("square","rectangle","circle","oval");
echo count($shapes);
?>

Different kind of Array :

There are three different kind of arrays as –

Type Description
Numeric array An array with a numeric index
Associative array An array with strings as index. This stores element values in association with key values rather than in a linear index order.
Multidimensional array An array containing one or more arrays and values are accessed using multiple indices