Skip to main content

Array and its types

What is an array?

An array is a data structure that stores one or more similar type of values in a single value.

There are three kinds of array.


1) Numeric array: It is a simple array with a numeric index and values are stored and accessed in linear fashion. It is also called Single dimensional array.
For example:
<?php
$a=array(11,23,45,67);
echo $a['1']."<br/>";
echo $a['2']."<br/>";
?>


2) Associative array: It is an array with strings as index. This stores element values in associatio with key values rather than in a linear fashion.
For example:
<?php
$salaries = array( 
           "Samir" => 2000, 
           "Gauri" => 1000, 
           "Shankar" => 5000
          );

echo "Salary of Samir is ". $salaries['Samir'] . "<br />";
echo "Salary of Gauri is ".  $salaries['Gauri']. "<br />";
echo "Salary of Shankar is ".  $salaries['Shankar']. "<br />";

?>



3) Multidimensional array: It is an array which contains more than one array and values are accessed using multiple indices.
 For example:
<?php
   $marks = array(
        "alex" => array
                (
                "physics" => 35,       
                "maths" => 30,       
                "chemistry" => 39       
                ),
        "rasmila" => array
                (
                "physics" => 30,
                "maths" => 32,
                "chemistry" => 29
                ),
        "shyam" => array
                (
                "physics" => 31,
                "maths" => 22,
                "chemistry" => 39
                )
         );
   /* Accessing multi-dimensional array values */
   echo "Marks for Alex in physics : " ;
   echo $marks['alex']['physics'] . "<br />";
   echo "Marks for Rasmila in maths : ";
   echo $marks['rasmila']['maths'] . "<br />";
   echo "Marks for Shyam in chemistry : " ;
   echo $marks['shyam']['chemistry'] . "<br />";
?>



Comments

Popular posts from this blog

MySQL Connection

Connection with MySQL Database Before accessing database, you must create a connection to the database Syntax: mysql_connect(servername,username,password); where, servername specifies the server to connect to. Default value is “localhost” username specifies the username to log in with. Default value is the name of the user that owns the server process. To connect offline we use username “root”. password specifies the password to log in with. Default is “” Code : Creating connection to the database and selecting the required database <?php $con = mysql_connect(“localhost”,”root”,”"); if (!$con) { die(‘Could not connect: ‘ . mysql_error()); } else{ mysql_select_db(“test”, $con) }; ?> Here, we have store connection in a variable called $con and trap error using die function. Closing connection The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function: <?php $con = mysql_conne...

Type Juggling and Type Casting

Type Juggling: PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

Doctype Defination

A Document Type Declaration, or DOCTYPE, is an instruction that associates a particular SGML or XML document (for example, a webpage) with a Document Type Definition (DTD). Syntax The general syntax for a document type declaration is: <!DOCTYPE root-element PUBLIC "FPI" ["URI"] [ <!-- internal subset declarations --> ]>