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...

Join in MySQL

Join clause is used to combine rows from two or more tables depending upon common field between them. There are different kinds of Joins. The most common and useful Join is Inner Join. Inner Join: It will return all rows from multiple tables where the condition is met.

Div and span

Div: Div <div> tag is that it divides the HTML document into sections. Proper usage of the <div> tag is fundamental to good HTML coding, the <div> tag is one of the most powerful tools available to a web developer. Span: Span <span> is  in-line level elements, they only span across small amounts of content, typically words or phrases. For example: a <span> tag might be used to make a word red in color or to give it an underline.