Creating Array in PHP

In this video tutorial, we are going to learn about What is an Array and creating an Array in PHP

Creating Array in PHP

In this video tutorial, we are going to learn about What is an Array and creating an Array in PHP. Basically an Array could be of three different types. Indexed arrays, Associative arrays and Multidimensional arrays, which are written in different ways. For example Indexed Array required numeric index id's. Where as an Associative Array referenced by named keys and in the third type of Array (Multidimensional Arrays) an Array contains more than one Array.

In general, an Array is a special kind of data type that allows you to store several pieces of information within it. Creating an array is extremely easy. It takes two parameters Key and Values and associates the values to keys. To create an Array, we first needs to write the Array function. See the syntax of an Array given below:

Syntax of Array:

$array = array (
key => value,
key2 => value2,
key3 => value3,
...
)

Always remember that in a single line Array declaration, each Array element must be separated by comma, however it is not mandatory to write comma after the last Array element that is omitted by an Array.

Now let?s take an example of creating an Array in PHP

Always remember that like variables in PHP, Array name also starts with the $ sing followed by array name and it takes to parameters key and values. Arrays are case sensitive and its name must be started from a letter not any other sign like underscore or number.

In this example we are going to store the name of users in an array. There is one more thing that one should remember that an array always starts at index Zero.
Example:

< ?php

$user[0] =" user 1";
$ user [1] =" user 2?;
$ user [2] =" user 3";
$ user [3] =" user 4";

Echo ?Users names are" . $ user [0] ."," . $ user [1] ."," . $ user [2] . ", and" . $ user [3];

?>

The output of the PHP Array Example, is as follows:

Users names are user 1, user 2,user 3 and user 4

Next: View all the tutorials of PHP.