PHP Tutorial

Your cool punch line

PHP Arrays

An array in PHP is actually an ordered map. A map is a type that maps values to keys. This type is optimized in several ways, so you can use it as a real array, or a list (vector), hashtable (which is an implementation of a map), dictionary, collection, stack, queue and probably more. Because you can have another PHP array as a value, you can also quite easily simulate trees.

There are three different kind of arrays:

1). Numeric Array
2). Associative Array
3). Multidimensional Array

Numeric Array

A numeric array stores each array element with a numeric index (ID Key).
There are different ways to create a numeric array.
1. In the following example the index are automatically assigned (the index starts at 0):


2. In the following example we assign the index manually:

$employee_array[0] = "Raj";
$employee_array[1] = "Sanjay";
$employee_array[2] = "Mahesh";
$employee_array[3] = "Ram";

PHP Example:-Use of ID key

$employee_array[0] = "Raj";
$employee_array[1] = "Sanjay";
$employee_array[2] = "Mahesh";
$employee_array[3] = "Ram";

echo $employee_array[0]." and ".$employee_array[1]."are friends."


Go Top

Associative Array

In an associative array a key is associated with a value. If you wanted to store the prize of your products in an array, a numerically indexed array would not be the best choice. Instead, we could use the products names as the keys in our associative array, and the value would be their respective prize.

PHP Example:-
$prize_array["Keyboard"] = 200;
$prize_array["Mouse"] = 180;
$prize_array["Monitor"] = 5000;
$prize_array["Speaker"] = 400;

echo "Keyboard prize is ".$prize_array["Keyboard"];


Go Top

Multidimensional Array

As each element value of the array can be any type it means that it can be other array as well. If an array element value is an other array then this is a multidimensional array. Of course the internal array values can be arrays as well and so on. You can define 10 dimensional (or more) array if you want.

PHP Example :-
We create a multidimensional array, with automatically assigned ID keys:

$families = array (
    "Prince"=>array ( "Raj", "Sanjay", "Prerna" ),
    "Ram"=>array ( "Rahul" ),
    "Sanjay"=>array ( "Sunny", "Tina", "Om" )
echo "Is " . $families['Prince'][2] . " a part of the prince family?";



PHP Introduction

PHP Basic Development

PHP Control Structures

PHP Functions

PHP Arrays

PHP File System

Working With PHP Forms

PHP Classes And Objects

Introduction To Database

PHP Cookies

PHP Session

Computer SMS

More Free SMS

Review on

© . All right Sitemap Overall Visits = 2209