Home Tutorial Php Php-variables-array PHP Array Variables

 
 

Share on Google+Share on Google+
PHP Array Variables
Posted on: January 9, 2010 at 12:00 AM
Advertisement
An array is a storage area of multiple values. Where you can add or remove multiple values with the help of array. In simple variable we can store only value at one time but in array variable we can store multiple values.

php variables array

An array is a storage area of multiple values. Where you can add or remove multiple values with the help of  array.  In simple variable we can store only value at one time but in array variable we can store multiple values. For example : If you want to keep the record of every cricket player in your diary. It means you cannot make distinct variable for every different  player but with the help of  array variable you can do it easily.

There are three kinds of array in Php.

1. Numeric Array

2. Associative Array

3 Multidimensional Array

1. Numeric Array (a)

An array defines key or index to store the array elements. A numeric array uses the numbers to store the array elements. There are two methods to term the numeric array. The index starts from 0. For example : 

<?php

$company = array("Sachin" , "Dhoni" , "Yuvraj" , "Sehwag");

echo $company[0]." "." and ".$company[3]." ". " are the most stylish player !";

?>

In the above example, index is automatically assign by the program.

 (b)

<?php

$player[0] = "Sachin Tendulkar";

$player[1] = "MSD";

$player[2] = "Sehwag";

$player[3] = "Yuvraj Singh";

echo $player[0]. " " . "is the best player in the world !" . "<br/>";

echo $player[1]. " " . "is the best captain in the world !." . "<br/>";

echo $player[2]. " " . "is the best best test player in the world !." . "<br/>";

echo $player[3]. " " . "is the best 20-20 player in the world !.";

?>

Here, we assign the value in the index one by one.

2. Associative Array

An array can be used in many ways. One of the popular method to create array is giving name to their keys or index. It is used when we have long list of records or persons. where we can't remember them by their index numbers. So, giving name to the keys is the easiest way to get the information about one particular person or object. There are two ways to create associative array. 

Example (1) :- 

<?php

$stationary = array("Item" => "Pencil", "Price" => "12.00", "Weight" => "10");

echo $stationary["Item"] . " " . " at the cost of Rs. " .$stationary["Price"] . " " . "and you will get" . $stationary["Weight"] . " " . " pieces. ";

?> 

The output is of the above example is : 

Pencil at the cost of Rs. 12.00 and you will get10 pieces.

Now you know how to define an associative array with the keys in subscript. It is easier to remember named than numbers.

Example (2) :-

<?php

$stationary["Item"] = "Pencil";

$stationary["Price"] = "Rs.12";

$stationary["Weight"] = "10 Pieces";

 

echo $stationary["Item"] . " " . " at the cost of" . " " . $stationary["Price"] . " " . " and you will get" . " " . $stationary["Weight"];

 

?>

 

The output is of the above example is : 

Pencil at the cost of Rs. 12.00 and you will get10 pieces.

You will get the same output but the style of defining array or keys is different. So, you can choose any of them according to your suitability. 

There are one more method to display the program by separating array keys and value. Let see the example below :

Example (3) :-

<?php

$stationary = array("Item" => "Pencil", "Price" => " Rs. 12", "Weight" => " 10 pieces.");

print_r($stationary);

?>

The output is of the above example is : 

Array ( [Item] => Pencil [Price] => Rs. 12 [Weight] => 10 pieces. )

Here, in this example print_r( ) function separates the keys and values automatically. 

2. Multidimensional Array

A multidimensional array is  an array which stores the value of another array. It means if you want to create an array the value of that array is another array and obviously the internal array might having few more values of array. So, don't get messed up and see the following example :-

Example: 1

<?php

$stationary = array( array( "Pencil", "Rs 12.00", "10 pieces"),

array( "Pen", "Rs 50.00", "10 pieces"),

array( "Notebook", "Rs 100", "10 pieces"),

array( "Books", "Rs 500", "10 pieces")

);

echo $stationary[0][0] . " " . "at the cost of" . $stationary[0][1] . " " . "and you will get" . $stationary[0][2] . "<br/>";

echo $stationary[1][0] . " " . "at the cost of" . $stationary[1][1] . " " . "and you will get" . $stationary[1][2] . "<br/>";

echo $stationary[2][0] . " " . "at the cost of" . $stationary[2][1] . " " . "and you will get" . $stationary[2][2] . "<br/>";

echo $stationary[3][0] . " " . "at the cost of" . $stationary[3][1] . " " . "and you will get" . $stationary[3][2];

?>

The output is of the above example is :

Pencil at the cost of Rs 12.00 and you will get10 pieces
Pen at the cost of Rs 50.00 and you will get10 pieces
Notebook at the cost of Rs 100 and you will get10 pieces
Books at the cost of Rs 500 and you will get10 pieces

Example : 2

<?php

$stationary = array( array("Item" => "Pencil", "Price" => "Rs 12.00", "Weight" => "10 pieces"),

array("Item" => "Pen", "Price" => "Rs 50.00", "Weight" => "10 pieces"),

array("Item" => "Notebook", "Price" => "Rs 100", "Weight" => "10 pieces"),

array("Item" => "Books", "Price" => "Rs 500", "Weight" => "10 pieces")

                                    );

           print_r($stationary);

?>

The output is of the above example is :

Array ( [0] => Array ( [Item] => Pencil [Price] => Rs 12.00 [Weight] => 10 pieces ) [1] => Array ( [Item] => Pen [Price] => Rs 50.00 [Weight] => 10 pieces ) [2] => Array ( [Item] => Notebook [Price] => Rs 100 [Weight] => 10 pieces ) [3] => Array ( [Item] => Books [Price] => Rs 500 [Weight] => 10 pieces ) )

Advertisement

Related Tags for PHP Array Variables :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 9, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com