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

CSS List

CSS allows you to customize the lists that can be made with HTML  to the extent that even images can be used as bullet points for unordered lists. CSS List Style Type If you want to use something different from the default numbering of ordered lists, or the bullets/discs of unordered lists, then all you have to do is choose a different style for your lists. CSS allows you to select from a wide variety of different list item shapes. Unordered list styles: square, circle, disc (default), and none Ordered list styles: upper-alpha, lower-alpha, upper-roman, lower-roman, decimal (default), and none Code:

Accessible Forms

Forms aren't the easiest of things to use for people with disabilities. Navigating around a page with written content is one thing, hopping between form fields and inputting information is another. Because of this, it is a good idea to add a number of elements to the form. Labels Each form field should have its own label. The label tag sorts this out, with a for attribute that associates it to a form element:

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.