how to define array in php

Array In php -:  We need to handle a large volume of data in terms of reading,processing and printing. to process such large amount of data we need a powerful data type that would facilitate efficient sorting accessing and manipulation of data items.

An array is a fixed size size sequenced collection of elements of the same data. In others words we can say “a collection of same data type in large value called array ”.An array stores multiple values in a single variable.  for example     array()

$x="cars";
$y="bus";
$z="train";

Types of array in php-: There are three type array available in php.

  1. single  array
  2. Two dimensional  array
  3. Multidimensional array

 

 

Single  array in php-: In one dimensional php array are use with numeric number so its called the numeric array or single array.  For example of single array

 <?php
         /* First method to create a array. */
         $numbers = array( 10, 20, 30, 40, 50);
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
         
         /* Second method to create array. */
         $numbers[0] = "ten";
         $numbers[1] = "twenty";
         $numbers[2] = "thirty";
         $numbers[3] = "forty";
         $numbers[4] = "fifty";
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
      ?>

 

The result of this program

Value is 10
Value is 20
Value is 30
Value is 40
Value is 50
Value is ten
Value is twenty
Value is thirty
Value is forty
Value is fifty

 

Two dimensional array in php –:  It means array are use index and name in php. It is store two array with name in php.  For example

 <?php
         /* First method to associate create array. */
         $price = array("gold" => 2000, "cooper " => 1000, "silver" => 500);
         
         echo "price  of gold is ". $price['gold']. "<br />";
         echo "price  of cooper is ". $price['cooper']. "<br />";
         echo "price  of silver ". $price['silver']. "<br />";
         
         /* Second method to create array. */
            $price['gold'] = "high";
            $price['cooper'] = "medium";
            $price['silver'] = "low";
         
         echo "price  of gold is ".$price['gold'] . "<br />";
         echo "price  of cooper is ".$price['cooper']. "<br />";
         echo "price  of silver is ".$price['silver']. "<br />";
      ?>

 

Output of this program

price of gold is 2000
price of cooper is 1000
price of silver 500
price of gold is high
price of cooper is medium
price of silver is low

 

Multidimensional array in php-: In the php support multiple type array. For example

array arrayname[a1][a2][a3]....[an]

For understanding we take a example of multidimensional array

<html>
<head>
<title> example of multidimensional array</title> 
</head>
<body>
<?php
         $marks = array( 
            "vikram" => array (
               "physics" => 35,
               "maths" => 30,	
               "chemistry" => 39
            ),
            
            "anil" => array (
               "physics" => 30,
               "maths" => 32,
               "chemistry" => 29
            ),
            
            "govind" => array (
               "physics" => 31,
               "maths" => 22,
               "chemistry" => 39
            )
         );
         
         /* Accessing multi-dimensional array values */
         echo "Marks for vikram in physics : " ;
         echo $marks['vikram']['physics'] . "<br />"; 
         
         echo "Marks for anil in maths : ";
         echo $marks['anil']['maths'] . "<br />"; 
         
         echo "Marks for govind in chemistry : " ;
         echo $marks['govind']['chemistry'] . "<br />"; 
      ?>
</body>
</html>

The output of this program

Marks for vikram in physics : 35
Marks for anil in maths : 32
Marks for govind in chemistry : 39

 

Leave a Reply

Your email address will not be published. Required fields are marked *