Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX

09/03/2014 Categories: PHP. No Comments on Arrays in PHP

Array is a collection of heterogeneous elements. There are two types of arrays in PHP. First type of array is a normal one that contains integer keys (indexes) which can be found in any typical programming languages. The second type of arrays is an associative array, where the keys are strings. Associative arrays are also known as hashes.

 

Array Creation

Arrays in PHP are dynamic. There is no need to specify the size of an array. A normal array can be created by using the integer index and the assignment operator as shown below:

$array1[0] = 10; 

If no integer index is specified, the index will be set to 1 larger than the previous largest index value used. Consider the following example:

$array2[3] = 5;

$array2[ ] = 90;

In the above example, 90 will be stored at index location 4.

There is another way for creating an array, using the array construct, which is not a function. The data elements are passed to the array construct as shown in the below example:

$array3 = array(10, 15, 34, 56);

$array4 = array( );

In the above example array4 is an empty array which can be used later to store elements.

A traditional array with irregular indexes can be created as shown below:

$array5 = array(3 => 15, 4 =>, 37, 5 => 23); 

The above code creates an array with indexes 3, 4 and 5.

An associative array which contains named keys (indexes) can be created as shown below:

$ages = array(“Ken” => 29, “John” => 30, “Steve” => 26, “Bob” => 28);

An array in PHP can be a mixture of both traditional and associative arrays.

 

Accessing Array Elements

Array elements can be accessed using the subscript (index or key) value which is enclosed in square brackets.

Consider a traditional array as shown below:

$array1 = array(10, 20, 30, 40); 

Third element (30) in the above array can be accessed by writing $array1[2]. The index of any traditional array starts with 0.

Consider an associative array or hash as shown below:

$ages = array(“Ken” => 29, “John” => 30, “Steve” => 26, “Bob” => 28); 

We can access the age (30) of John by writing $ages[‘John’].

 

Iterating through Arrays

The foreach loop can be used to print / access the values in a traditional or an associative array.

Consider a traditional array as shown below:

$array1 = array(10, 20, 30, 40); 

The foreach loop to print the values in the above array can be written as shown below:

Consider an associative array or hash as shown below:

$ages = array(“Ken” => 29, “John” => 30, “Steve” => 26, “Bob” => 28); 

The foreach loop to print the keys and values in the above array can be written as shown below:

 

Array Functions

The array elements can be manipulated or accessed in different ways. PHP has an extensive list of predefined functions that comes in handy while working with arrays. Some these functions are mentioned below:

array-functions

 

A video tutorial demonstrating arrays in PHP:

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Latest posts by Suryateja Pericherla (see all)

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me